将参数传递给Angularjs指令模板ng如果
是否可以在ng if中向指令模板传递参数 index.html:将参数传递给Angularjs指令模板ng如果,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,是否可以在ng if中向指令模板传递参数 index.html: <my-directive showName="John"></my-directive> template.html: <h1 ng-repeat="customer in customers" ng-if="customer.name === showName "> {{customer.name}} is {{customer.age}} </h1> {{cust
<my-directive showName="John"></my-directive>
template.html:
<h1 ng-repeat="customer in customers" ng-if="customer.name === showName ">
{{customer.name}} is {{customer.age}}
</h1>
{{customer.name}}是{{customer.age}
理想情况下,上面的示例只显示John的数据。
也许这不是正确的方法?
因为当我在指令中使用scope:{showName='@'}
时,ng repeat
似乎不再起作用了
scope: {
showName:'='
}
发生了两件事:
show name
。不showName
和ng repeat
,则应在ng repeat
上使用过滤器,而不是使用ng if
和ng repeat
。否则,您将遇到优先级问题(ng如果在ng repeat
之前执行ng)。一旦使用
scope: {
showName:'='
}
发生了两件事:
指令的参数定义为名称show name
。不showName
指令作用域被隔离。这意味着它无法访问父范围中定义的客户。您还必须在参数中传递它们
此外,如果和ng repeat
,则应在ng repeat
上使用过滤器,而不是使用ng if
和ng repeat
。否则您将遇到优先级问题(ng,如果在ng repeat
之前执行ng)。您应该改用过滤器
要使其工作,您必须更改模板中的表达式:
<h1 ng-repeat="customer in customers | filter:{name: showName}">
{{customer.name}} is {{customer.age}}
</h1>
但是,由于您有一个独立的作用域,您还应该像这样添加属性customers:
<my-directive show-name="John" customers="customers"></my-directive>
你应该改用过滤器
要使其工作,您必须更改模板中的表达式:
<h1 ng-repeat="customer in customers | filter:{name: showName}">
{{customer.name}} is {{customer.age}}
</h1>
但是,由于您有一个独立的作用域,您还应该像这样添加属性customers:
<my-directive show-name="John" customers="customers"></my-directive>
你有一个错误。因为在与ng repeat相同的级别上使用ng if,所以如果表达式为false(这是初始情况),整个表达式将不会执行。因此,您应该创建一个嵌套的div或类似的东西。因为在与ng repeat相同的级别上使用ng if,所以如果表达式为false(这是初始情况),整个表达式将不会执行。因此,您应该创建一个嵌套的div或类似的东西。