Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs: $scope update when I change a select with jQuery

I'm using a jQuery plugin to 'customize' my selects.

This plugin fires the change event of the original select when some option is selected.

The problem is that my scope doesn't change.

Here you can see a quick example... changing the select the scope changes. clicking the buttons the select changes but not the scope.

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

What am I missing?

like image 266
user2071887 Avatar asked Aug 07 '13 20:08

user2071887


2 Answers

You need to access the scope of the dropdown and then apply it as shown below:

$('button').on('click', function(){
    var newVal = $(this).data('val');
    $('select').val(newVal).change();

    var scope = angular.element($("select")).scope();
    scope.$apply(function(){
        scope.selectValue = newVal;
    });
});
like image 156
AlwaysALearner Avatar answered Sep 19 '22 02:09

AlwaysALearner


In your jQuery put auto trigger e.g

 $('#input').click(function(){
      $('#input').val('1');
      $('#input').trigger('input'); //add this line this will bind $scope Variable
    });
like image 30
Dinesh Sharma Avatar answered Sep 22 '22 02:09

Dinesh Sharma