简体   繁体   English

AngularJS:Ng-Model和Ng-Change在控制台中提供未定义

[英]Angularjs: Ng-Model and Ng-Change giving undefined in console

I have a dropdown that is defined using angular's ng-options syntax. 我有一个使用angular的ng-options语法定义的下拉菜单。 I also have ng-model on the element and I am trying to get the selected model to output to the console. 我在元素上也有ng-model,我正在尝试将所选模型输出到控制台。 I have read a lot of different SO questions, but I just keep getting undefined. 我已经阅读了很多不同的SO问题,但是我一直都不确定。 When I have the model defined in a service on rootScope then I get null. 当我在rootScope的服务中定义了模型时,我得到了null。

Not sure what is going on, but it seems like the object isn't getting into the ng-model. 不知道发生了什么,但似乎该对象没有进入ng-model。

In the view: 在视图中:

<select ng-model="selected" ng-change="updateCategory()" 
ng-options="conference.Name for conference in Adminconferences 
| orderBy:['Name'] "></select>

In the controller: 在控制器中:

$scope.updateCategory = function () {
    console.log($scope.selected);
}

Update: I realized that maybe if i include what i am trying to do then maybe I can get better responses and have a better question. 更新:我意识到,也许如果我包括我想做的事情,那么也许我可以获得更好的答复并提出更好的问题。

This dropdown list is located in the header of my site. 此下拉列表位于我的网站的标题中。 I want the user to be able to select which conference they want to manage. 我希望用户能够选择他们要管理的会议。 Based on that, I want all of the data in the site to be based on the conference's Id. 基于此,我希望站点中的所有数据都基于会议ID。 I am not sure what the angular way of doing this is. 我不确定这样做的角度方式是什么。 I think i need to use rootScope and just reference it whenever i need the id for data calls. 我认为我需要使用rootScope并在需要进行数据调用的ID时引用它。 Is this acceptable? 这可以接受吗?

I think you need to initialize $scope.selected in the controller before you use it inside function. 我认为您需要先在控制器中初始化$ scope.selected,然后才能在函数中使用它。 Initialize it like this: 像这样初始化它:

$scope.selected = 0;

and then use it inside the function. 然后在函数内部使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM