In my use case I could not bind, and triggering "input" or "change" did not
work. After digging and testing a variety of approaches, I finally found it:
Inside the link function, do:
element.controller('ngModel').$viewChangeListeners[0]();
In my case the directive is on an <input>.
Full example: trigger ng-change on blur:
wpModule.directive('triggerChangeOnBlur', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.on('blur', function () {
element.controller('ngModel').$viewChangeListeners[0]();
});
}
};
}]);
On Wednesday, February 13, 2013 at 2:03:02 AM UTC+1, José Rodolfo Freitas
wrote:
>
> Hello,
>
> I'm developing my first directive and I just found out that
> $(someElement).trigger('change') won't change the model value in angular.
>
> At reference [1] the author points that we have to manually fire an
> *input* event.
> how are you guys achieving that inside your directives? I've digged around
> and could find a simple way of doing that.
>
> Actually at whe using directive link function, I can't event trigger the
> change method through jquery $(someElement).trigger('change').
>
> for example. After mapping the bind above
>
> link: function(scope, element, attrs) {
> element.bind('change', function () {
> alert("change");
> })
> }
>
> Changing the component value through normal inputs calls the*
> alert("change") inside the bind function,* but with a
> $(component).trigger('change') call won't work.
> Could anyone give any light about my problem?
>
> Best Regards,
> José
>
>
> References:
> [1]
> http://deansofer.com/posts/view/14/AngularJs-Tips-and-Tricks-UPDATED#trigger-change
> :
>
> "If you've worked with form inputs containing ng-model, you may at a
> certain point start to wonder why some plugins or even
> $.fn.trigger('change') isn't updating the model. After some digging
> through the source
> <https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L382-406>,
>
> we found that Angular doesn't actually watch the change event by default.
> Instead it watches the new input event. Read up about the differences here
> <http://stackoverflow.com/questions/6458840/on-input-change-event#answer-6458946>
> .
>
> A simple hack we did for the AngularUI passthrough directive was to bind
> to the *change* event and manually fire an *input* event, however I
> recommend instead trying to leverage the new *input* event where you can
> instead."
>
>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.