带有 ng 重复的角度选择选项
Angular select options with ng-repeat
我已经为ngOptions,ngRepeat而苦苦挣扎了几天。我必须做一些事情,比如选择人员控制选项。我的 REST 服务在下面的代码片段中给了我类似数组的东西。我尝试了 Angular 文档中的每个堆栈溢出答案和示例。没有结果。我什至不能显示一个属性 - 我做错了吗?
下面有一些尝试。
感谢您的任何建议和帮助。
var myApp = angular.module('myApp',[]);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
$scope.name = 'Superhero';
$scope.players = [{"id": "1", "nickName" : "Cole", "firstName" : "Mark", "lastName" : "Coleman"}, {"id" : "2", "nickName" : "West", "firstName" : "Peter", "lastName" : "West"}];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form>
<label>Captain:
<select ng-model="team.captain">
<option ng-repeat="player in players" value="{{player.id}}"> {{player.firstName}} {{player.lastName}}</option>
</select>
</label><br />
</form>
您忘记在应用程序中注册控制器:angular.module('myApp').controller('MyCtrl', MyCtrl);
然后在 html 中的模板中设置<html ng-app="myApp"></html>
.
控制器控制所需的模板,例如<form ng-controller='MyCtrl'>
<!DOCTYPE html>
<htm ng-app="myApp">
<body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form ng-controller="MyCtrl">
.. <select ng-model="team.captain">
<option ng-repeat="player in players" value="{{player.id}}"> {{player.firstName}} {{player.lastName}}</option>
</select>
</form>
<script>
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.name = 'Superhero';
$scope.players = [{"id": "1", "nickName" : "Cole", "firstName" : "Mark", "lastName" : "Coleman"}, {"id" : "2", "nickName" : "West", "firstName" : "Peter", "lastName" : "West"}];
}
myApp.controller('MyCtrl',MyCtrl);
</script>
</body>
</html>
您需要为myApp
模块注册控制器
喜欢:
var myApp = angular.module('myApp',[]);
myApp.controller('myCtrl', MyCtrl);
function MyCtrl($scope) {
$scope.name = 'Superhero';
$scope.players = [{"id": "1", "nickName" : "Cole", "firstName" : "Mark", "lastName" : "Coleman"}, {"id" : "2", "nickName" : "West", "firstName" : "Peter", "lastName" : "West"}];
}
在 HTML 中,您应该使用ng-app="myApp"
和ng-controller="myCtrl"
喜欢:
<html ng-app="myApp"> //used module
<head>
// load your script here
</head>
<body ng-controller="myCtrl"> // used controller
<form>
<label>Captain:
<select ng-model="team.captain">
<option value ="">select one</option>
<option ng-repeat="player in players" value="{{player.id}}"> {{player.firstName}} {{player.lastName}}</option>
</select>
</label><br />
</form>
</body>
</html>
普伦克演示链接
亲爱的,试试这个:
应用程序.JS代码
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.name = 'Superhero';
$scope.players = [{"id": "1", "nickName" : "Cole", "firstName" : "Mark", "lastName" : "Coleman"}, {"id" : "2", "nickName" : "West", "firstName" : "Peter", "lastName" : "West"}];
$scope.selected = $scope.players[0];
}
网页代码
<select ng-options="player as player.nickName for player in players ng-model="selected"></select>
- 工作小提琴 这是你的代码:在 AngularJs 小提琴中选择
谢谢和干杯
相关文章:
- CKeditor:更改对话框中的默认选择选项
- 如何在从多选下拉列表中选择选项值时动态生成文本框
- AngularJs列表ng单击以选择选项转换
- 使用基于文本的 Jquery 选择选项
- 当选择值x时,Javascript需要在正确的位置显示文本区域,从而循环通过具有选择选项的表单
- 在php和mysql中选择选项.一旦选择了选项,就列出我的sql数据库数据
- 选择选项时如何显示警报
- 使用php动态更改选择选项
- 选择“选择选项”时显示文本.怎么做
- LEAFLET地图和带有选择选项的标记-ANGULARjs
- 使用js将动态内容添加到选择选项列表中
- 用于使用javascript循环选择选项
- AngularJS-选择选项并在函数中使用它
- 禁用/启用基于单选/选择选项所选Javascript的文本框
- 如何使用jQuery隐藏与选择选项相关的选择选项列表
- 选择选项是添加或删除事件
- 使用jQuery向“选择选项”添加默认属性
- 构建HTML选择字段并使用JavaScript数组选择选项
- 带有动态选择选项的Firefox html5验证异常
- 只有当Angular从资源对象检索数据时,才选择选项元素