AngularJS $ tracks cost changes after initialization

I have a situation where I need to fix an event when the model changes, but initialization in my directive fires the event too early.

Plunker: http://plnkr.co/edit/2T0Rq6yOXHWxQAOv8RpX?p=preview

How can I overcome this? Setting some flags, but how / where? Other approaches?

Thanks!

UPDATE

I added an attribute valueto the directive so that you can better understand what I mean by initialization.

Plunker: http://plnkr.co/edit/a5fzJi7VzAytj3Urj06L?p=preview

+4
source share
3 answers

I finally decided it!

Plunker: http://plnkr.co/edit/a5fzJi7VzAytj3Urj06L?p=preview

form Angular $dirty. false, , ngModel "". ngModel , form.

PS. , , .

+3

data

$scope.data = { x: 51 }

Angular , : y z, undefined. 0 . ,

{ x: 51, y: 0, z: 0 }

, $scope.data , . .

, data y z:

$scope.data = { x: 51, y: 0, z: 0 };

: http://plnkr.co/edit/RfztKvrxhzPYMoSss7Fm?p=preview

+1

You can check if a property exists for an object. demo

 if ((newValue !== oldValue) && oldValue.x) {

or perhaps a more accurate demonstration :

if ((newValue !== oldValue) && (typeof oldValue.x !== 'undefined')) {
0
source

Source: https://habr.com/ru/post/1525068/


All Articles