我有一个指令,将使用ng-model
来公开其值。
问题在于,这个指令有一些内部组件也会影响作用域,所以我需要隔离它的作用域,但仍然要保留ng-model
与外部世界的绑定。
我应该如何实现这个?
这是指令:
angular.module('app', []).directive('myDirective', function() {
return {
restrict: 'E',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
// do stuff using ngModel controller
},
replace: true,
template: '<div><input ng-model="userInput" /></div>'
};
});
<my-directive ng-model="prop"></my-directive>
如您所见,指令必须将
prop
暴露为其值,但不应在父级作用域中暴露在<input ng-model="userInput"/>
中定义的userInput
。我该如何使只有
prop
在父级作用域中可用?
prop
?它会成为一个字段元素的一部分吗?此外,你展示的ngModel
是用于prop
还是userInput
? - Mark Rajcokng-model
。其中一个来自输入,将是用户输入的内容。我的指令控制器将$watch
它的变化,使用ngModelController.$setViewValue()
补充和更新prop
。我知道我可以监听输入事件并避免内部ng-model
,但在未来,我将有更多的元素共享相同的内部作用域属性。因此,我想要一个隔离的作用域,让内部元素可以玩耍,但仍然希望将ng-model="prop"
绑定到父作用域。我还将使用外部ng-model
的有效性功能。 - Caio Cunha