12

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?

5 Answers 5

27

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;
    });
});
Sign up to request clarification or add additional context in comments.

Comments

11

When you click the button, angular goes out of its scope and uses jquery to manipulate the data/to perform some action, so we need to exlicitly call $scope.$apply() to reflect the changes back into the scope of the controller. And change your controller to this:

app.controller('AppCtrl', function($scope) {
    $('button').on('click', function(){
        $scope.selectValue=$(this).data('val');
        $scope.$apply();
    });
}

By the way you can use jquery event inside the angular..

Comments

3

Ideally in angularJS, controller should not update DOM reference directly. If you want to achieve same thing, you should expose one method over $scope and use "ng-click" directive.

If you want to achieve same using jQuery, it should go into directive as

$scope.$apply()

to update the scope.

Comments

2

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
    });

Comments

1

It's best not to mix DOM manipulation with Angular. Try the following for your button HTML:

<button class="setVal" ng-click="selectValue=1">Set 1</button>
<button class="setVal" ng-click="selectValue=2">Set 2</button>
<button class="setVal" ng-click="selectValue=3">Set 3</button>

I tried the above in your Plunker and it worked.

4 Comments

This will stop working if you use both the buttons and the select. Click around and you will see.
The problem is that the layout comes from the jQuery plugin. I cant change the markup. :S
If you can't control the markup, you can still modify it using jQuery after it is generated: <script> $('button.setVal').each(function(index, item) { $(item).attr('ng-click', 'selectValue=' + $(item).data('val')); }); </script> Also make sure the buttons are inside the div that is bound to the controller. I did not have sushain97's problem.
If my comments along with my answer works for you, could you please consider marking it as the answer? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.