由于某种原因,绑定(bind)在 ng-if
内的输入上不起作用在指令中阻止
所以这不起作用:
app.directive 'foo', ->
restrict: 'E'
scope:
type:'='
template: "<input ng-if=\"type === 'string'\" ng-model='filterText'>
<div> {{filterText}} </div>"
<foo type="'string'" />
它在指令之外或没有
ng-if
的情况下都能正常工作.使用 ng-if
在 div 内包装输入没有帮助。它是一个错误吗?jsbin link
最佳答案
这是由于 ng-if 引入了一个新的范围以及您的 ng-model “其中没有点”这一事实引起的。
这有效:
template: "<div ng-init='holder={}'> <input ng-if=\"type === 'string'\" ng-model='holder.filterText'></div>
<div> {{holder.filterText}}</div>"
请参阅 https://docs.angularjs.org/api/ng/directive/ngIf 上的指令信息并注意文本“此指令创建新范围”。
对于“模型中的点”,请参见例如
Does my ng-model really need to have a dot to avoid child $scope problems?
或者
https://egghead.io/lessons/angularjs-the-dot
基本上,在读取值时,它将通过范围原型(prototype)正确读取,但在修改值时,它将被写入自己的范围。
关于angularjs - 绑定(bind)停止在 ng-if 中的输入中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26107621/