Reputation: 203
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?
Upvotes: 2
Views: 8590
Reputation: 8246
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) {
}
};
}
Upvotes: 5