简体   繁体   English

ng-model作用域与ng-options一起使用时不可用

[英]ng-model scope not available while using with ng-options

I am just trying to implement a simple drop down and here is my code 我只是想实现一个简单的下拉列表,这是我的代码

<select ng-change="selectTestSuite();" ng-model="testCase" ng-options="testSuite.TEST_NAME for testSuite in publicTestCase"></select>

In the controller when I am trying to print value of testCase in console it is giving undefined. 在控制器中,当我尝试在控制台中打印testCase的值时,它的值未定义。 But When I try to print id of testCase using 但是当我尝试使用以下命令打印testCase的ID时

{{testCase.TEST_ID}}

It is giving me id. 它给了我身份证。 I have also checked by using $watch 我也用$ watch检查过

$scope.$watch('testCase',function() {
   console.log($scope.testCase);
});

Unable to figure out where I am making mistake.Appreciate any help. 无法弄清楚我在哪里出错。感谢任何帮助。

I think no problem with ng-options. 我认为ng-options没问题。

Actually ng-options maps the "testSuite.TEST_NAME" as model to ng-model. 实际上ng-options将“ testSuite.TEST_NAME”作为模型映射到ng-model。

Ensure the "testSuite.TEST_NAME" is available in "publicTestCase" collection (Whether it is undefined). 确保“ publicTestCase”集合中的“ testSuite.TEST_NAME”可用(是否未定义)。

You are binding the testCase to testSuite.TEST_NAME. 您正在将testCase绑定到testSuite.TEST_NAME。 Your ng-options should look like this: 您的ng-options应该看起来像这样:

<select ng-change="selectTestSuite()" ng-model="testCase" ng-options="testSuite as testSuite.TEST_NAME for testSuite in publicTestCase"></select>

The model gets bind to the value of testSuite from the records in the publicTestCase array and for each of the testSuite you want the testSuite.TEST_NAME to be shown as the options text. 模型从publicTestCase数组中的记录绑定到testSuite的值,对于每个testSuite,您都希望将testSuite.TEST_NAME显示为选项文本。

enter image description here 在此处输入图片说明

Note:- Please find the image link.image contains code ng-repeat directive repeats a block of HTML code for each item in an array, it can be used to create options in a dropdown list, but the ng-options directive was made especially for filling a dropdown list with options, and has at least one important advantage: 注意:-请找到图像链接。image包含代码ng-repeat指令为数组中的每个项目重复一段HTML代码,可用于在下拉列表中创建选项,但是ng-options指令特别针对用于使用选项填充下拉列表,并且至少具有一个重要优点:

Dropdowns made with ng-options allows the selected value to be an object, while dropdowns made from ng-repeat has to be a string. 使用ng-options进行的下拉列表允许所选值成为对象,而通过ng-repeat进行的下拉列表则必须是字符串。

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

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