简体   繁体   English

如何观察或观察AngularJS服务中的输入属性更改?

[英]How to observe or watch an input attribute change in an AngularJS Service?

I have a radio button in my UI that has an ngModel and it enable/disable another input through ngDisabled. 我的UI中有一个带有ngModel的单选按钮,它通过ngDisabled启用/禁用另一个输入。

I want to observer/watch/trigger when an input element become enabled/disabled and it has to be done in an Angular Service. 当输入元素被启用/禁用时,我想观察/观察/触发,并且必须在Angular Service中完成。 I simply can't find how to do that. 我根本找不到怎么做。 Here is how I would do it in a Directive: 以下是我将如何在指令中执行此操作:

// for the case of field that might be ng-disabled, we should skip validation
// Observe the angular disabled attribute
attrs.$observe("disabled", function(disabled) {
  if (disabled) {
    // Turn off validation when element is disabled
    cancelValidation();
  } else {
    // Re-Validate the input when enabled
    var value = ctrl.$viewValue || '';
    ctrl.$setValidity('validation', validate(value, true));
  }
});

But how to do it in a Service ??? 但是如何在服务中做到这一点???
I do not wish to have another Directive, I really want it done through the Service. 我不希望有另一个指令,我真的希望通过该服务完成。

Before asking the question, I found this Is it possible to 'watch' attributes' changes and I tried it 在提出这个问题之前,我发现这可以“观察”属性的变化 ,我尝试了

scope.$watch(function() {return element.attr('disabled'); }, function(newValue){});

but that does not seem to work, at least not when I enable/disable my radio button (the input element as an ngDisabled that is bind to the radio button with an ngModel) 但这似乎不起作用,至少在我启用/禁用我的单选按钮时(输入元素作为ngDisabled与ngModel绑定到单选按钮)

I also found that we could possible use jQuery.watch() but I want to stick with vanilla javascript or jqLite. 我还发现我们可以使用jQuery.watch(),但我想坚持使用vanilla javascript或jqLit​​e。 Is it possible? 可能吗?

EDIT 编辑
I should have mentioned that this Service is on it's own and does not have access to the Controller, because it's a Form validation tool and it does not know about the outside (at least not more than the element itself and his scope). 我应该提到这个服务是独立的,并且无法访问Controller,因为它是一个表单验证工具,它不知道外部(至少不超过元素本身和他的范围)。 You can check it out and use it if you want: Angular-Validation and it support Directive and Service depending on what or where you want the validation to happen and my problem was related to the code in the Service only. 如果需要,您可以查看并使用它: Angular-Validation ,它支持指令和服务,具体取决于您希望验证发生的位置和位置,我的问题仅与服务中的代码相关。 The answer from pankajparkar helped me solved it... Thanks :) pankajparkar的答案帮我解决了......谢谢:)

I believe you had attribute disabled with an interpolation something like disabled="{{disable}}" that why you were able to put $observe on that value try changing the same thing to $watch will not work on that attribute , you need to additionally need $interpolate service to evaluate you attribute interpolation. 我相信你的属性已disabled插值,例如disabled="{{disable}}"为什么你能够将$observe放在该值上尝试将同样的东西更改为$watch将不适用于该attribute ,你需要另外需要$interpolate服务来评估属性插值。 So that the actually value will get evaluate is it disabled or '' and if changes it state then $watch function will get fired accordingly. 因此,实际值将被评估是disabled还是''并且如果更改它状态,那么$watch函数将相应地被触发。

Code

scope.$watch(function() {
   return $interpolate(element.attr('disabled'))(scope); //this will evaluate attribute value `{{}}``
}, function(newValue){
    //your code would be here
});

The purpose of a Scope is to "glue together" the presentation and the business logic of your app. Scope的目的是将应用程序的表示和业务逻辑“粘合在一起”。 It does not make much sense to pass a $scope into a service. $scope传递给服务没有多大意义。 You could always do a $scope.$watch at the initialisation of the controller using this service (the one associated with the view of your radio button) and trigger a call to the service to do the update you need. 您可以随时使用$scope.$watch使用此服务(与单选按钮视图关联的服务)初始化控制器,并触发对服务的调用以进行所需的更新。


If you can already access this variable in your service (getting it from the controller) you should be able to do : 如果您已经可以在服务中访问此变量(从控制器获取),您应该能够:

app.service('myservice',function(){
    this.listen = function($scope) {
        $scope.$watch(function(){return someScopeValue},function(){
           //$scope.dosomestuff();
        });
    }
});

//your controller

function myCtrl($scope,myservice) {
    $scope.listen = function() {
        myservice.listen($scope);
    }

    //call your method
    $scope.listen();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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