由于某种原因,绑定不适用于指令中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 -w包含div内部的输入.这是一个错误吗?
解决方法
它是由ng-if引入一个新的范围和你的模型“没有点在其中”这一事实引起的.
这有效:
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中的指令信息,并注意文本“此指令创建新范围”.
对于“dot-in-model”,请参阅示例
Does my ng-model really need to have a dot to avoid child $scope problems?
要么
https://egghead.io/lessons/angularjs-the-dot基本上在读取值时,将正确读取范围原型,但在修改值时,它将被写入自己的范围.