Lucas Batistussi
Lucas Batistussi

Reputation: 2343

Is there any way to proper order events in angularjs?

I am having a lot of headaches trying to make $watch executes before event on an element of a form (a checkbox to be more precise). The only browser that works is FF.

                scope.$watch(attrs.ngModel, function($v){

                    console.log("1 = "+$v);

                });

                // elem corresponds to the element in form (i.e.: checkbox)
                elem.bind('change', function(evt){

                    console.log("2 = "+control.$modelValue);

                    scope.$digest();
                }); 

enter image description here

Is there a way to do this work in all browsers?

Upvotes: 0

Views: 235

Answers (1)

haki
haki

Reputation: 9759

event are raised before the DOM was rendered (to allow manipulation) so if you want to use the values in the field after they were rendered you can use $timeout.

For example , check if a form field is dirty in a directive ...

elm.bind('keydown', function(event) {              
     if (event.which === 13) 
          scope.update();

     scope.check_dirty();
});

scope.check_dirty = function() {
    // wait for the DOM to finish rendering
    $timeout(function() {
        if ( elm.val().trim() !== scope.last_saved_value )
            elm.addClass('calc-dirty-input');
        else
            elm.removeClass('calc-dirty-input');
    },0);
};  
...

Upvotes: 1

Related Questions