繁体   English   中英

手表在angularJs的指令链接内部无法正常工作

[英]watch is not working inside directive link in angularJs

当错误消息时,应禁用“保存”按钮

<user-textbox name="txtbox" error-Message="errormsg"></user-textbox>
<button  id="modalSaveButton" type="button" ng-click="save()" data-dismiss="{{ errormsg ? '' : 'modal' }}" value="Save"></button>

指示

            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:即使保存按钮出错,也不会禁用它

因此,您在指令标记中编写了:

<user-textbox name="txtbox" error-Message="errormsg"></user-textbox>

您能否在标记中将属性名称更改为错误消息。 所以看起来像:

<user-textbox name="txtbox" error-message="errormsg"></user-textbox>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM