Javascript 如果使用默认选择选项,如何读取ng模型数据?
我使用以下代码绑定了成本中心信息,并设置了默认值 控制器: 我有一些逻辑来填充成本中心列表。 假设它具有以下数据:Javascript 如果使用默认选择选项,如何读取ng模型数据?,javascript,angularjs,Javascript,Angularjs,我使用以下代码绑定了成本中心信息,并设置了默认值 控制器: 我有一些逻辑来填充成本中心列表。 假设它具有以下数据: $scope.costCenterList=[{ "costCenter": 111 }, { "costCenter": 112 }, { "costCenter": 113 }, { "costCenter": 114 }]; $scope.selected = $scope.costCenterList[0]; HTML: 输出: Selected costCenter:
$scope.costCenterList=[{ "costCenter": 111 }, { "costCenter": 112 }, { "costCenter": 113 }, { "costCenter": 114 }];
$scope.selected = $scope.costCenterList[0];
HTML:
输出:
Selected costCenter: 111
它打印作为默认值呈现给表单的数据。它确实显示了我使用表单选择的数据
这里有什么指导吗?我想你想要这个:
ng-options="item as item.costCenter for item in costCenterList"
下面是一个JSFIDLE:
我用一个按钮更新了小提琴,这样你就可以看到它的价值了
<select placeholder="Cost Center" ng-options="item as item.costCenter for item in costCenterList" ng-model="selected" ng-init="selected=costCenterList[0]">
您可以在JSFIDLE的更新版本中看到这一点:
我想你想要这个:
ng-options="item as item.costCenter for item in costCenterList"
下面是一个JSFIDLE:
我用一个按钮更新了小提琴,这样你就可以看到它的价值了
<select placeholder="Cost Center" ng-options="item as item.costCenter for item in costCenterList" ng-model="selected" ng-init="selected=costCenterList[0]">
您可以在JSFIDLE的更新版本中看到这一点:
你试过这个吗?
成本中心
带控制器:
$scope.costCenterList=[{“costCenter”:111},{“costCenter”:112},{“costCenter”:113},{“costCenter”:114}];
$scope.selected=$scope.costCenterList[0] 你试过这个吗?
成本中心
带控制器:
$scope.costCenterList=[{“costCenter”:111},{“costCenter”:112},{“costCenter”:113},{“costCenter”:114}];
$scope.selected=$scope.costCenterList[0] 运行“console.log”(“Selected costCenter:+$scope.Selected”);”时会发生什么因此,我的costCenterList是一个JSON对象,其内容如下:
code
{{{“costCenter”:111},{“costCenter”:112},{“costCenter”:113},{“costCenter”:114}code
。。。。。。。。。。。。。。。。。。。。。。。。因此,当我打印$scope.selected.costCenter时,它总是打印111。当您运行“console.log”(“selected costCenter:+$scope.selected”);”时会发生什么因此,我的costCenterList是一个JSON对象,其内容如下:code
{{{“costCenter”:111},{“costCenter”:112},{“costCenter”:113},{“costCenter”:114}code
。。。。。。。。。。。。。。。。。。。。。。。。所以当我打印$scope.selected.costCenter时,它总是打印111。我试过你的例子。它在小提琴页面中起作用。但在我的应用程序中,它仍然会继续打印默认选择的内容,即111。很难解决无法复制的问题。但是我猜变量被设置在适当的位置,在做出选择后被反复触发。您可以尝试在ng init中设置变量。我会在答案中加上这个…哦-你在你的版本中更改了ng选项,对吗?我试过你的例子。它在小提琴页面中起作用。但在我的应用程序中,它仍然会继续打印默认选择的内容,即111。很难解决无法复制的问题。但是我猜变量被设置在适当的位置,在做出选择后被反复触发。您可以尝试在ng init中设置变量。我会把它添加到答案中…哦-你在你的版本中更改了ng选项,对吗?