简体   繁体   English

Angularjs使用ng-repeat隔离范围指令

[英]Angularjs isolates scope directive with ng-repeat

I'm trying to use directive on ng-repeat items each with an isolate scope but it isn't working. 我正在尝试使用ng-repeat项目的指令,每个项目都有一个隔离范围,但它不起作用。 I'm looping through each item and coloring it red with the inboxuser-select directive. 我循环遍历每个项目并使用inboxuser-select指令将其着色为红色。 However, when I put the directive on, it doesn't show any of my scope values. 但是,当我打开指令时,它不显示任何我的范围值。 What is the issue here? 这是什么问题? Thanks 谢谢

html file html文件

   <li class="inbox-chatter"  data-ng-    
        repeat="inboxuser in inboxusers">
        <p inboxuser-select selected={{inboxuser}}">{{inboxuser}}</p>
       </li>

directive.js directive.js

.directive('inboxuserSelect', function() {
return {
    restrict: 'A',
    scope: {
        selected: "@"
    },
    link: function(scope, element, attrs) {
   scope.selected.css('color','red');
    }

}
 });

The problem is that once you set an isolate scope on the directive then the whole DOM element has that isolate scope. 问题是,一旦在指令上设置了隔离范围,那么整个DOM元素就具有隔离范围。 So the inboxuser from your ng-repeat is no longer in scope when data binding occurs (it's on the parent scope). 因此,当发生数据绑定时,来自ng-repeatinboxuser不再在范围内(它位于父范围内)。

One option is to set scope to true instead of using an isolate scope so you'll inherit everything from the parent scope. 一种选择是将scope设置为true而不是使用隔离范围,因此您将继承父范围内的所有内容。

Or you can stick with an isolate scope, but pass inboxuser in to the directive and display it using a template. 或者您可以使用隔离范围,但将inboxuser传递给指令并使用模板显示它。 Since you're already passing inboxuser in to the directive's scope through selected it'd be easy to just add this to your directive: 由于您已经通过selectedinboxuser传递到指令的范围,因此很容易将其添加到您的指令中:

   template: '{{selected}}',

Also, by the way, you're missing a quote on your <p> . 顺便说一句,你错过了<p>上的引用。 So this might work better for you (note I also removed {{inboxuser}} from within the <p> assuming you'll be using the template to display that instead): 所以这可能对你{{inboxuser}} (注意我也从<p>删除了{{inboxuser}} ,假设你将使用模板来显示它):

  <p inboxuser-select selected="{{inboxuser}}"></p>

To be honest, I don't understand what you really need to do but I have a feeling that this design will not get you there. 说实话,我不明白你真正需要做什么,但我觉得这个设计不会让你到那里。

However, I fixed your example just for the purposes of explaining how things work. 但是,我修复了你的例子只是为了解释事情是如何工作的。

You can see it live here . 你可以在这里看到它。

So... when you write: 所以...当你写:

scope: {
    selected: "@"
}

you are actually saying that my isolated scope will hold a single property named selected which will be of type string and will contain whatever {{inboxuser}} evaluates to. 你实际上是在说我的隔离范围将包含一个名为selected属性,它将是string类型,并包含{{inboxuser}}求值的内容。 And not only this, whenever inboxuser changes in the outter scope, selected will also change in the inner, isolated scope. 而且不仅如此,每当inboxuserinboxuser范围内发生变化时, selected内容也会在内部隔离范围内发生变化。 This is how '@' binding works. 这就是'@'绑定的工作原理。

Whatever you put nested in <p inboxuser-select selected="{{inboxuser}}"></p> , is binded to that isolated scope, which does not have an inboxuser property. 无论您嵌套在<p inboxuser-select selected="{{inboxuser}}"></p> ,都绑定到该隔离范围,该范围没有inboxuser属性。 So, it has to change to: 所以,它必须改为:

<p inboxuser-select selected="{{inboxuser}}">{{selected}}</p>

Finally, scope.selected.css('color','red'); 最后, scope.selected.css('color','red'); should be changed to: 应改为:

element.css('color','red');

The element argument in link function is the DOM element where the directive instance is applied. link函数中的element参数是应用指令实例的DOM元素。 scope.selected is just a string. scope.selected只是一个字符串。

I suggest you rething your overall design. 我建议你重新整理你的整体设计。 If you need help, feel free to ask. 如果您需要帮助,请随时提出。

If it helps you, you can use AngScope , a tiny firebug extention i've written. 如果它对你有帮助,你可以使用AngScope ,这是我写的一个小小的萤火虫扩展。 It's just a quick way to inspect $scope instances associated to DOM elements inside firebug's DOM inspector. 它只是一种快速检查与firebug的DOM检查器中的DOM元素相关联的$scope实例的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM