简体   繁体   English

Angular $ scope。$ watch查找仅从形式更改的变量(ng-model指令)

[英]Angular $scope.$watch for variables that changed only from the form (ng-model directive)

I have a listener that sends HTTP request to a server when I have changes in a model. 我有一个侦听器,当模型发生更改时,该侦听器会将HTTP请求发送到服务器。

JS code: JS代码:

$scope.a = 1;
$scope.$watch('a', function(newValue, oldValue) {
    sendSaveRequest(newValue);
});

HTML form: HTML形式:

<form>
    <input ng-model="a" />
</form>

This code works fine. 此代码可以正常工作。 But I also have a web socket connection with my web server. 但是我的Web服务器也有Web套接字连接。 And if somebody else changes this model everybody should update this model, but they don't need to send the request to the server in this case: 而且,如果其他人更改了该模型,则每个人都应更新此模型,但是在这种情况下,他们不需要将请求发送到服务器:

socket.on('change', function(newValue) {
    $scope.a = newValue;
    $scope.$apply();
});

How to listen the model changes that occured only from the HTML form (ng-model directive)? 如何侦听仅从HTML表单(ng-model指令)发生的模型更改?

Use the ng-change directive: 使用ng-change指令:

<form>
    <input ng-model="a" ng-change="sendSaveRequest(a)"/>
</form>
$scope.sendSaveRequest = sendSaveRequest;

The ngChange expression is only evaluated when a change in the input value causes a new value to be committed to the model. 仅当输入值的更改导致将新值提交给模型时才评估ngChange表达式。

It will not be evaluated: 不会被评估:

  • if the model is changed programmatically and not by a change to the input value 如果通过编程方式而不是通过更改输入值来更改模型

For more information, see AngularJS ng-change Directive API Reference . 有关更多信息,请参见《 AngularJS ng-change Directive API Reference》

When model value is changing ,template is updating.When change in the template,it is written to the console immediately. 当模型值更改时,模板正在更新。当模板值更改时,它将立即写入控制台。

Sample link for ng-model directive: http://jsfiddle.net/BtrZH/168/ ng-model指令的示例链接: http : //jsfiddle.net/BtrZH/168/

you just put the socket change out of the digest cycle like below. 您只需将套接字更改从摘要循环中取出,如下所示。 In this case watcher will not be called and hence no server call. 在这种情况下,不会调用监视程序,因此不会调用服务器。

socket.on('change', function(newValue) {
  setTimeout(function() {
      $scope.a = newValue;
  }, 1000);
});

You can use Angular's ng-blur directive on your input instead of adding the watch in your controller. 您可以在输入中使用Angular的ng-blur指令,而不是在控制器中添加手表。

In HTML: 在HTML中:

<input ng-model="a" ng-blur="onBlur()"/>

The $scope.ngBlur function (defined below) will be called only after user changes the value manually on the form. 仅在用户在表单上手动更改值之后,才会调用$ scope.ngBlur函数(定义如下)。 Changing the value programatically (ie $scope.a = newValue;) will not call the function. 以编程方式更改值(即$ scope.a = newValue;)将不会调用该函数。

In controller: 在控制器中:

$scope.a = '';
$scope.oldValue = $scope.a;

$scope.onBlur = function() {
  if ($scope.a !== $scope.oldValue) {
      $scope.oldValue = $scope.a;
      console.log('Request sending with value: ' + $scope.a);
      sendSaveRequest($scope.a);
  }
};

socket.on('change', function(newValue) {
    $scope.a = newValue;
    $scope.$apply();
});

Here you can find the plunker: http://plnkr.co/edit/iczEWILyp5y0QuZBML80 . 在这里,您可以找到塞子: http ://plnkr.co/edit/iczEWILyp5y0QuZBML80。

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

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