2017-06-12 38 views
0

我有模型node如何使用指令進行模型輸入?

{"id":"593d408cb25f42a812000042","prefix":"array_simple", 
"title":"Array_simple","type":"3","value":["A","B","C"],"nodes":[]} 

在這種obejct存在具有值作爲陣列字段值:

"value":["A","B","C"] 

如何使用指令來顯示在陣列中的每個值輸入和結合這對模型更改後返回?

我試着寫指令:

.directive('inputArray', function ($compile) { 
    return { 
     restrict: 'E', 
     scope: { 
      data: "=" 
     }, 

     link: { 
      pre: function (scope, element) { 

       scope.inputs = []; 

       if (angular.isArray(scope.data.value)) { 
        angular.forEach(scope.data.value, function (value) { 
         scope.inputs.push({"type": scope.data.type, "value": value}); 
        }); 

       } else { 
        scope.inputs.push({"type": scope.data.type, "value": scope.data.value}); 
       } 
      } 
     }, 
     template: `<input ng-repeat="value in inputs track by $index" 
        ng-model="node.value" 
        ng-hide="value.type=='2' || value.type=='6'" 
        type="text" value="$$value.value$$">`, 
     replace: true 
    } 
}) 

但更改後不會改變模型回來。

回答

0

之前試圖執行一個指令,得到的東西只用模板和控制器的工作:

<script src="//unpkg.com/angular/angular.js"></script> 
 
    <body ng-app> 
 
    <div ng-init='data = {"id":"593d408cb25f42a812000042","prefix":"array_simple", 
 
"title":"Array_simple","type":"3","value":["A","B","C"],"nodes":[]}'> 
 
    </div> 
 

 
    <div ng-repeat="item in data.value track by $index"> 
 
     <input ng-model="data.value[$index]" type="text" /> 
 
    </div> 
 

 
    <p>{{data}}</p> 
 
    <p>{{data.value}}</p> 
 
    </body>

而是將數據複製到新的數據模型,使用直接使用$index數據模型和property accessor bracket notationdata.value[$index]

相關問題