Angularjs 如何从嵌套元素获取指令内部的ng模型值?
如何在嵌套HTML元素的链接函数中获取ngModel的值,即:Angularjs 如何从嵌套元素获取指令内部的ng模型值?,angularjs,Angularjs,如何在嵌套HTML元素的链接函数中获取ngModel的值,即: <div myattr> <label>Title</label> <input type="text" ng-model="need.value.of.this"> </div> 您可以使用scope从DOM中获取ngModel值,并在链接函数中访问该值您可以将watcher添加到您的作用域中 link: function ($scope, $element, $a
<div myattr>
<label>Title</label>
<input type="text" ng-model="need.value.of.this">
</div>
您可以使用
scope
从DOM
中获取ngModel值,并在链接
函数中访问该值您可以将watcher添加到您的作用域中
link: function ($scope, $element, $attrs) {
$scope.$watch('need.value.of.this', function(newValue) {
// do something with new value
});
}
您可以在myattr上注册ng模型,就像ng模型将自己注册到ng form elementHi一样,我的ng模型可以不同。。。在ngModel上设置“need.value.of.this”对我来说太具体了。$viewValue我解决了这个问题。。。但是现在我有了嵌套的元素,而ngModel是未定义的)。。。有解决办法吗?
app.directive('myattr', [
function () {
return {
restrict: 'EA',
scope: {
ngModel: '=',
},
link: function ($scope, $element, $attrs) {
// need ng-model value from input
}
}
}
}
link: function ($scope, $element, $attrs) {
$scope.$watch('need.value.of.this', function(newValue) {
// do something with new value
});
}