2017-07-28 31 views
0
下面

添加模態值是HTML:如何連結/與現有JSON對象在角JS

<tr ng-repeat-start="select in selection"> 
    <td style="overflow: hidden;">{{select.name}}</td> 
    <td style="overflow: hidden;">{{select.type}}</td> 
    <td style="overflow: hidden;">{{select.application}}</td> 
    <td> 
     <image ng-src="{{commentImg}}" width="20" height="20" ng-click="selectComment()"></image> 
    </td> 
</tr> 

selectComment打開模態具有文本區域。選擇數組每個對象有3個鍵/值(即名稱,類型,應用程序)。我想添加textarea值作爲每個對象的第四個鍵/值來選擇json對象。

下面

是模態: -

<div class="dialog-panel3"> 
    <div class="page-title"> 
     Comments 
    </div> 
    <br> 
    <form name="commentForm"> 
     <textarea class="textarea" ng-model="inputValue" required></textarea> 
     <br> 
     <br> 
     <button ng-click="comment(inputValue)" ng-disabled="commentForm.$invalid">Save</button>&nbsp;&nbsp 
     <button ng-click="close()">Cancel</button> 
    </form> 

</div> 

請建議。

回答

1

單擊打開模式的圖像,可以保存JSON對象的相應索引。使用索引 爲您的textarea創建ng模型,以幫助您將該值與現有的JSON數組對象關聯。

HTML

<tr ng-repeat-start="select in selection"> 
    <td style="overflow: hidden;">{{select.name}}</td> 
    <td style="overflow: hidden;">{{select.type}}</td> 
    <td style="overflow: hidden;">{{select.application}}</td> 
    <td> 
     <image ng-src="{{commentImg}}" width="20" height="20" ng-click="selectComment($index)"></image> 
    </td> 
</tr> 

位指示

$scope.selectComment=function(index){ 
    $scope.selection[index].textAreaVal=""; 
    $scope.selectedIndex=index; 
} 

莫代爾

<div class="dialog-panel3"> 
    <div class="page-title"> 
     Comments 
    </div> 
    <br> 
    <form name="commentForm"> 
     <textarea class="textarea" ng-model="selection[selectedIndex].textAreaVal" required></textarea> 
     <br> 
     <br> 
     <button ng-click="comment(selection[selectedIndex].textAreaVal)" ng-disabled="commentForm.$invalid">Save</button>&nbsp;&nbsp 
     <button ng-click="close()">Cancel</button> 
    </form> 

</div> 
+0

如何保存JSON對象的相應索引在點擊Ø f圖像,當我點擊保存按鈕時,它應該在json對象中添加textarea值作爲新的鍵/值。,請您詳細說明 – ABA

+0

當您單擊圖像時,此函數將被調用selectComment(),將相應的$從ng-repeat索引並將其保存在一個變量(我的答案中的selectedIndex)中,並用它來映射你的texarea ng-model到JSON對象 – Vivz

+0

@ABA它現在在工作嗎? – Vivz