AngularJS安排了一组指令

时间:2014-05-24 00:16:02

标签: javascript angularjs

如果我有指令的ng-repeat:

<div class="container">
  <div ng-repeat="p in people">
    <my-element></my-element>
  </div>
</div>

两个问题:

  1. 如何将模型p传递给指令?
  2. 如何修改所有指令&#39; myElement&#39;?例如,如果它们都有图像,我需要使用bin打包算法设置它们的绝对位置。
  3. 由于

1 个答案:

答案 0 :(得分:2)

您在指令中想要的任何内容都可以通过$scope传递。您可以在templateUrl文件或template属性中指定样式:

.directive('myThing', function() {
  //
  //<--it's perfectly valid to do any computations and/or setup here-->
  //
  return {
    restrict: 'E',
    scope: {
      ngModel: '=', //<--this links ng-model attribute into directive
      myP:'='//<--this links my-p attribute into directive
    },
    link: function(scope, element, attrs, tabsCtrl) {
      tabsCtrl.addPane(scope);
    },
    templateUrl: 'my-thing.html' //<--you could specify style in the template
  };
});

所以 - 您可以将p作为模型或my-p属性传递:

<my-element my-p="p"></my-element>