动态ng模型值不适用于双向数据绑定

时间:2014-12-19 06:43:03

标签: angularjs angularjs-scope angularjs-ng-repeat

我对角js很新。

我使用动态字段创建了表单。我需要在这里创建动态ng-model值,如Field [lable.LabelID] lable.LabelID是动态值

这是我的模板页面

 <form ng-submit="" ng-controller="saveOrder">
  <table class="col-xs-12 col-sm-8 col-sm-offset-4">
     <tr ng-repeat="lable in Grp.Label">
       <td style="width:30%" class="no-wrap text-left col-xs-text-center text-primary">
        <h4>{{lable.LabelName}}</h4>
       </td>
       <td style="width:60%" class="no-wrap  text-left">
           <input type="{{input_type[lable.InputType]}}" 
           name="{{lable.LabelID}}" ng-model="Field[lable.LabelID]" 
            ng-init="Field[lable.LabelID] = Field[lable.LabelID] || {}"/>
       </td>
    </tr>
 </table>

我的控制器就像

 productApp.controller('saveOrder',function($scope,$http)
{
    $scope.FieldOptions = {};
    $scope.createOrder = function()
    {
       alert("in");
       console.log($scope);
       console.log($scope.Field[1]); 
       console.log($scope.FieldOptions);
    }

 });

我的服务器数据就像

{
   "GroupName": "Order Form",
   "Label": [
     {
        "InputType": "1",
        "LabelID": "1",
        "LabelName": "BillingDetails"
     },
     {
        "InputType": "1",
        "LabelID": "2",
        "LabelName": "shipping Details"
     }
    ]
 }

对于那种情况,我得到的错误就像

  TypeError: Cannot set property '1' of undefined

at Function.v.assign (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:73:162)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:72:33
at Object.e.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:88:347)
at pre (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:145:309)
at i (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:43:396)
at e (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:39:307)
at e (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:39:324)
at e (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:39:324)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:38:372
at Object.<anonymous> (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:148:122) <input type="{{input_type[lable.InputType]}}" name="{{lable.LabelID}}" ng-model="Field[lable.LabelID]" ng-init="Field[lable.LabelID] = Field[lable.LabelID] || {}" class="ng-pristine ng-valid">

我发现这些类型的问题很多。但我无法为我的方案找出解决方案。请帮我解决这个问题...

任何类型的帮助都很明显。

1 个答案:

答案 0 :(得分:1)

尝试在控制器内设置$scope.Field = [];并删除ng-init指令。在您当前的代码中,编译发生时$scope.Field未定义。