我如何使用在后端(Symfony)上选择的选项来初始化我的选择。现在我初始化我的选择(ng-model="myselect"
示例),例如 ng-init="myselect='0'"
我需要在 ng-init 中设置实际选定的选项,但是如果我删除 ng-init 指令,它会创建值为 的空选项?未定义:未定义?
我该怎么办?
最佳答案
<div ng-app="myapp">
<fieldset ng-controller="FirstCtrl">
<select
ng-options="p.id as p.first + ' ' + p.last for p in people"
ng-model="selectedPerson"></select>
{{ selectedPerson }}
</fieldset>
var myapp = angular.module('myapp', []);
myapp.controller('FirstCtrl', function ($scope) {
$scope.selectedPerson = 2;
$scope.people = [
{ id: 1, first: 'John', last: 'Rambo', actor: 'Silvester' },
{ id: 2, first: 'Rocky', last: 'Balboa', actor: 'Silvester' },
{ id: 3, first: 'John', last: 'Kimble', actor: 'Arnold' },
{ id: 4, first: 'Ben', last: 'Richards', actor: 'Arnold' }
];
});
这就是你所期望的.. 另请检查此链接 http://jsfiddle.net/kaehejgo/
关于javascript - 使用 Angularjs 从后端获取所选选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42687778/