2

I am using angularjs, I have a directive which shows list of questions. I intend to do some logic when selectedQuestion changes. So, I used ng-change.

This is my directive:

 restrict: 'EA',
            scope: {                             
                selectedQuestion: '=?', 
                questionsList: '='  
            },
            link: function (scope) {
                scope.change = function () {
                    if (scope.selectedQuestion) {
                        //do something
                    }
                };
            }

This is the html template for my directive:

   <select  ng-model="selectedQuestion"
            ng-options="avail.question for avail in questionsList track by avail.id" ng-change="change()"
   </select>

  {{selectedQuestion}}

When I change the question in the UI list, the {{selectedQuestion}} changes to show the json of the question selected. However the scope.selectedQuestion in scope.change function is always the same as initialized (not changing).

What is causing this issue?

1 Answer 1

5

Try passing model as parameter to the ng-change function:

<select ng-model="selectedQuestion"
        ng-options="avail.question for avail in questionsList track by avail.id" 
        ng-change="change(selectedQuestion)"
</select>

Then in your directive:

link: function (scope) {
        scope.change = function (currentQuestion) {
            if (currentQuestion) {
            }
        };
     }
Sign up to request clarification or add additional context in comments.

Comments

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.