Cod*_*dec 2 javascript angularjs angularjs-directive
我正在使用 angularjs,我有一个显示问题列表的指令。我打算在变化时做一些逻辑selectedQuestion。所以,我用了ng-change.
这是我的指令:
restrict: 'EA',
scope: {
selectedQuestion: '=?',
questionsList: '='
},
link: function (scope) {
scope.change = function () {
if (scope.selectedQuestion) {
//do something
}
};
}
Run Code Online (Sandbox Code Playgroud)
这是我的指令的 html 模板:
<select ng-model="selectedQuestion"
ng-options="avail.question for avail in questionsList track by avail.id" ng-change="change()"
</select>
{{selectedQuestion}}
Run Code Online (Sandbox Code Playgroud)
当我更改 UI 列表中的问题时,{{selectedQuestion}}更改会显示所选问题的 json。然而scope.selectedQuestionin scope.change函数始终与初始化时相同(不变)。
是什么导致了这个问题?
尝试将模型作为参数传递给ng-change函数:
<select ng-model="selectedQuestion"
ng-options="avail.question for avail in questionsList track by avail.id"
ng-change="change(selectedQuestion)"
</select>
Run Code Online (Sandbox Code Playgroud)
然后在你的指令中:
link: function (scope) {
scope.change = function (currentQuestion) {
if (currentQuestion) {
}
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8233 次 |
| 最近记录: |