Javascript Angularjs:在下拉列表中定义运行时的列
我有一个下拉列表,它绑定到json对象 我想在运行时定义下拉列表将绑定到的列名。请帮忙 代码段:Javascript Angularjs:在下拉列表中定义运行时的列,javascript,angularjs,Javascript,Angularjs,我有一个下拉列表,它绑定到json对象 我想在运行时定义下拉列表将绑定到的列名。请帮忙 代码段: <!DOCTYPE html> angular.module(“myapp”,[]) .controller(“HelloController”,函数($scope){ $scope.helloTo={}; $scope.selectedVal=“name”; $scope.helloTo.title=“世界,AngularJS”; $scope.phones=[{ “名
<!DOCTYPE html>
angular.module(“myapp”,[])
.controller(“HelloController”,函数($scope){
$scope.helloTo={};
$scope.selectedVal=“name”;
$scope.helloTo.title=“世界,AngularJS”;
$scope.phones=[{
“名称”:“Nexus S”,
“snippet”:“Nexus S的速度更快了。”,
年龄:1
}, {
“名称”:“摩托罗拉XOOM”™ 使用Wi-Fi’,
“snippet”:下一代平板电脑,
年龄:2
}, {
“名称”:“摩托罗拉XOOM”™',
“snippet”:下一代平板电脑,
年龄:3岁
}];
});
我想定义如下内容$scope.selectedVal
<select ng-options="$scope.selectedVal for p in phones" ng-model="p.name"></select>
试试这个
<select ng-options="p[selectedVal] for p in phones" ng-model="selectedOption"></select>
您可以使用以下代码执行此操作
<div ng-controller="MyCtrl">
<select ng-options="p.name for p in phones" ng-model="selectedValue"></select>
</div>
function MyCtrl($scope) {
$scope.phones = [
{
'name': 'Nexus S',
'snippet': 'Fast just got faster with Nexus S.',
'age': 1
},
{
'name': 'Motorola XOOM™ with Wi-Fi',
'snippet': 'The Next, Next Generation tablet.',
'age': 2
},
{
'name': 'MOTOROLA XOOM™',
'snippet': 'The Next, Next Generation tablet.',
'age': 3
}
];
$scope.selectedValue = $scope.phones[0];
}
函数MyCtrl($scope){
$scope.phones=[
{
“名称”:“Nexus S”,
“snippet”:“Nexus S的速度更快了。”,
年龄:1
},
{
“名称”:“摩托罗拉XOOM”™ 使用Wi-Fi’,
“snippet”:下一代平板电脑,
年龄:2
},
{
“名称”:“摩托罗拉XOOM”™',
“snippet”:下一代平板电脑,
年龄:3岁
}
];
$scope.selectedValue=$scope.phones[0];
}
这就是您的示例。不确定,但请检查这是否有效:@HarishR:是的,这对我有效,但在ng模型中定义什么?
<div ng-controller="MyCtrl">
<select ng-options="p.name for p in phones" ng-model="selectedValue"></select>
</div>
function MyCtrl($scope) {
$scope.phones = [
{
'name': 'Nexus S',
'snippet': 'Fast just got faster with Nexus S.',
'age': 1
},
{
'name': 'Motorola XOOM™ with Wi-Fi',
'snippet': 'The Next, Next Generation tablet.',
'age': 2
},
{
'name': 'MOTOROLA XOOM™',
'snippet': 'The Next, Next Generation tablet.',
'age': 3
}
];
$scope.selectedValue = $scope.phones[0];
}