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?
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?
Share Improve this question edited Nov 1, 2017 at 14:21 Codec asked Oct 31, 2017 at 20:42 CodecCodec 2131 gold badge3 silver badges7 bronze badges1 Answer
Reset to default 5Try 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) {
}
};
}