最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs: ng-change for select list - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 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) {
            }
        };
     }
发布评论

评论列表(0)

  1. 暂无评论