angularjs-ng-change

When to use $watch or ng-change in Angularjs

可紊 提交于 2019-11-27 11:55:19
问题 When should I use angular $watch functions and when use ng-change angularjs directive? To me, they both can do the same. Are there any differences or usage patterns between them? 回答1: They are not the same, clearly. One is used solely in the controller; the other is a directive on an input element. But even in their application they differ. When you use $watch the watched expression will be evaluated on every digest cycle, and if there is a change, the handler is invoked. With ng-change , the

How can I make angularjs ngChange handler be called only when user finishes typing

天大地大妈咪最大 提交于 2019-11-27 09:46:31
问题 I have an input field, where I want to apply the variant of ngChange . The input field is sort of binding with an ajax call, when user changes the input, the server side will process the data, however, I don't wanna make the call too often. Say the user wanna input a really string, I want the call be made only after user finishes the word he is about to type. Nevertheless, I don't wanna use event such as blur. What would be a better way to implement this, rather than setTimeout ? 回答1: Use ng

How can I denote which input fields have changed in AngularJS

风格不统一 提交于 2019-11-26 19:49:07
问题 I'm working on an edit form (user.html) that PUTs data to an API, but I'd like to avoid PUTting all the data in the form. I'd like to PUT just the changed items. I've seen the use of dirty and pristine when working with forms, but this applies to any change in the form. I've also seen the use of ng-change, but I don't want to trigger an action on a change to one element, just denote that the changed element should be included in the PUT. Anyone found a way to denote only the input fields that

How to implement an ng-change for a custom directive

南楼画角 提交于 2019-11-26 15:57:27
问题 I have a directive with a template like <div> <div ng-repeat="item in items" ng-click="updateModel(item)"> <div> My directive is declared as: return { templateUrl: '...', restrict: 'E', require: '^ngModel', scope: { items: '=', ngModel: '=', ngChange: '&' }, link: function postLink(scope, element, attrs) { scope.updateModel = function(item) { scope.ngModel = item; scope.ngChange(); } } } I would like to have ng-change called when an item is clicked and the value of foo has been changed

Angularjs dynamic ng-pattern validation

旧时模样 提交于 2019-11-26 10:19:33
I have a form that if a checkbox is false enforces validation on a text input using the ng-required directive. If the checkbox is true, the field is hidden and the ng-required is set to false. The problem is that I also have a regex for validation specified on the input as well utilizing the ng-pattern angular directive. The issue I am running into is that if a user fills in an invalid phone number, checks the box to deactivate that input (and consequently needs no further validation) the form will not allow submission as it is invalid based on the ng-pattern. I attempted to resolve this issue

Angularjs dynamic ng-pattern validation

假如想象 提交于 2019-11-26 02:06:40
问题 I have a form that if a checkbox is false enforces validation on a text input using the ng-required directive. If the checkbox is true, the field is hidden and the ng-required is set to false. The problem is that I also have a regex for validation specified on the input as well utilizing the ng-pattern angular directive. The issue I am running into is that if a user fills in an invalid phone number, checks the box to deactivate that input (and consequently needs no further validation) the