[英]watch is not working inside directive link in angularJs
Save Button shoud get disable when their is an error message 当错误消息时,应禁用“保存”按钮
<user-textbox name="txtbox" error-Message="errormsg"></user-textbox>
<button id="modalSaveButton" type="button" ng-click="save()" data-dismiss="{{ errormsg ? '' : 'modal' }}" value="Save"></button>
Directive 指示
link(scope) {
scope.errormsg = false;
function disableSave (n, o) {
const eleModalSave =
angular.element(document.querySelector('#modalSaveButton'));
if(scope.errormsg) {
eleModalSave.attr('disabled',"");
//save button should get disable if errormsg is true
} else {
eleModalSave.removeAttr('disabled');
}
}
scope.$watch('errormsg',disableSave, true);
}
O/P: Save button is not getting disable even if their is an error O / P:即使保存按钮出错,也不会禁用它
so in the directive markup you have written: 因此,您在指令标记中编写了:
<user-textbox name="txtbox" error-Message="errormsg"></user-textbox>
Could you please change attribute name to error-message in the markup. 您能否在标记中将属性名称更改为错误消息。 So it looks like:
所以看起来像:
<user-textbox name="txtbox" error-message="errormsg"></user-textbox>
Angular will translate this on the scope to $scope.errorMessage, which means your directive declaration should look like: Angular会将其在范围内转换为$ scope.errorMessage,这意味着您的指令声明应类似于:
.directive('userTextbox', function(){
scope : {
errorMessage :'='
},
link : ['$scope', function($scope){
$scope.$watch('errorMessage', function(newVal, oldVal) {
})]
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.