ng-model - 绑定到对象中的动态字段

时间:2017-03-08 02:17:35

标签: angularjs angular-ngmodel angularjs-ng-model

我有这段代码:

  <div ng-repeat="c in q.children | orderBy:[]">

      <div ng-if="c.kind == 'text'">
        <label>
          {{c.value}}
          <textarea ng-model="q.newResponse.value['{{c._id}}']"></textarea>
        </label>
      </div>

  // ...

我要做的是绑定newReponse.value上的属性,其中该属性是动态的,并且是c._id值,如下所示:

newResponse.value.44PkfeoakfoAf5o3r3773oZS3a = 'foo bar baz';

这不能正常工作,我收到此错误:

Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 22 of the expression [q.newResponse.value[{{c._id}}]] starting at [{c._id}}]].
http://errors.angularjs.org/1.6.1/$parse/syntax?p0=%7B&p1=invalid%20key&p2=22&p3=q.newResponse.value%5B%7B%7Bc._id%7D%7D%5D&p4=%7Bc._id%7D%7D%5D
    at angular.js:68
    at AST.throwError (angular.js:14893)
    at AST.object (angular.js:14882)

这是可能的,这是做正确的方法吗?

1 个答案:

答案 0 :(得分:0)

没关系,这不是那么难 -

      <div ng-if="c.kind == 'text'">
        <label>
          {{c.value}}
          <textarea ng-model="q.newResponse.value[c._id]"></textarea>
        </label>
      </div>