Вопрос

I'm trying to use a ng-repeat to dynamically pass in an object value to a directive, like so:

<td ng-repeat="attr in demographicAttrs">
  <yes-no name="{{ attr }}-{{ $index }}"
    model="ambassador.demographic.{{ attr }}"
    update="calcScore(ambassador)"></yes-no>
</td>

where the directive is defined as

function (
  $rootScope
) {
  "use strict";

  return {
    restrict: 'E',
    replace: true,
    templateUrl: 'templates/yesNo.html',
    scope: {
      "name": "@",
      "model": "=",
      "update": "&"
    },
    link: function(scope, element, attrs) {
      scope.$watch('model', function(value) {
        scope.update();
      });
    }
  };
};

So I'm trying to dynamically generate the model to be watched in ambassador.demographic.{{ attr }}, but the = 2 way binding for directives, doesn't like the value being generated that way.

I get this error: https://docs.angularjs.org/error/$parse/syntax?p0=%7B&p1=is%20an%20unexpected%20token&p2=24&p3=ambassador.demographic.%7B%7B%20attr%20%7D%7D&p4=%7B%7B%20attr%20%7D%7D

How would I do this?

Updates

Это было полезно?

Решение

Change the model expression to use the javascript bracket notation:

model='ambassador.demographic[attr]'

Fiddle

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top