如何在属性中添加角度表达式?

时间:2017-05-24 18:29:49

标签: angularjs angularjs-directive

我有一个使用options属性进行配置的指令:

<div something something-options="{
    'count': 'item-0'
}">

这是在ng-repeat中。如何将repeat $ index添加到options属性中?这些不起作用:

<div something something-options="{
    'count': 'item-'+$index
    'count': 'item-'+{{$index}}
}">

示例:http://plnkr.co/edit/VMXdjR7TJCZGd5ly4Bdl

1 个答案:

答案 0 :(得分:0)

  

在您的示例中,您尝试将对象作为字符串,但在此示例中,我们尝试将其作为$ scope中的变量。

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.b = [1,2,3];
});

app.directive("something", function() {
  return {
    scope: {
      somethingOptions: "="
    },
    link: function($scope) {
      console.log($scope.somethingOptions);
    }
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <div ng-app="plunker" ng-controller="MainCtrl">
    <div data-ng-repeat="a in b" something something-options="{
    'count': 'item-'+$index
}">{{a}}</div>
  </div>