根据您的问题,我编写了一个听起来像您想要的工作示例。看看它,如果我错过了什么,请告诉我。
我设置了一个登陆页面,列出了一些学生和他们的书籍。我使用路由在视图之间传递数据以设置编辑页面。我使用 ng-options 指令列出书籍并相应地绑定它们。
演示插件
Javascript:
angular.module('plunker', [])
.config(function($routeProvider) {
$routeProvider.when('/list', {
templateUrl: 'list.html',
controller: 'ListCtrl'
})
.when('/edit/:book', {
templateUrl: 'edit.html',
controller: 'EditCtrl'
})
.otherwise({
redirectTo: '/list'
});
})
.service('BookService', function($q, $timeout) {
var unassigned = ['English', 'History'];
this.getUnassigned = function() {
var deferred = $q.defer();
// Simulate async call to server.
$timeout(function() {
deferred.resolve(unassigned);
});
return deferred.promise;
};
})
.controller('EditCtrl', function($scope, $routeParams, BookService) {
// student.book needs to be set to avoid null select option
$scope.student = {book: $routeParams.book, name: $routeParams.name };
$scope.unassigned = BookService.getUnassigned().then(function(data) {
return [$routeParams.book].concat(data);
});
})
.controller('ListCtrl', function($scope) {
$scope.students = [{
name: 'Billy',
book: 'Math'
}, {
name: 'Joe',
book: 'Science'
}];
});
HTML:
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.1.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js" data-semver="1.1.5"></script>
<script src="app.js"></script>
<script type="text/ng-template" id="list.html">
<table>
<theader>
<tr><td>Name</td><td>Book</td><td></td></tr>
</theader>
<tbody>
<tr ng-repeat="student in students">
<td>{{ student.name }}</td><td>{{ student.book }}</td><td><a ng-href="#/edit/{{ student.book }}?name={{student.name}}">Edit</a></td>
</tr>
</tbody>
</table>
</script>
<script type="text/ng-template" id="edit.html">
<div>
<p>Current Student: {{ student.name }}</p>
<label>Select A Book: </label>
<select ng-model="student.book" ng-options="book for book in unassigned">
</select>
<p> You have selected: {{student.book}}</p>
</script>
</head>
<body>
<div ng-view></div>
</body>
</html>