我正在玩 ngRoute 和 Angular,似乎在尝试在视图中选择一个项目时遇到问题,以便转到包含 coshed 项目的详细信息页面:
<h1>Select Item</h1>
<ul>
<li ng-repeat="item in vm.items">
<a ng-click="vm.setSelectedItem(item)" ng-href="#/first/{{item.id}}">
{{item.thing}}
</a>
</li>
</ul>
为什么vm.selectedItem
没有在“详细信息”页面上呈现/显示?
var app=angular.module("myApp", ['ngRoute']);
app.config(function($routeProvider){
$routeProvider
.when('/first', {
templateUrl: 'first.html',
controller: 'FirstCtrl',
controllerAs:'vm'
})
.when('/first/:id', {
templateUrl: 'firstWithItem.html',
controller: 'FirstCtrl',
controllerAs:'vm'
})
.otherwise({
redirectTo: '/first'
});
});
app.controller("FirstCtrl", function() {
var vm = this;
vm.items = [{id:1, thing:"Beer"},{id:2, thing:"Grass"}];
vm.selectedItem = null;
vm.setSelectedItem = SetSelectedItem;
function SetSelectedItem(item) {
this.selectedItem = item;
}
});