我正在使用 Yeoman - 角度发生器。
JSBin:JSBin 链接
我有一个从工厂设置angApp.factory("Airports", function() {});
并从 ng-repeat 显示的简单机场列表<ul ng-repeat="airport in airports.detail">
。
我想进行交互,当单击每个链接时,它将匹配机场代码并将其显示在新的段落标签<p class="current">Current: {{currentAirport.name}}</p>
中。
setAirport(airport.code)
为什么在html中单击机场链接时该功能不会设置currentAirport.name
(或显示?)?
控制器
angular.module("ang6App")
.controller("AirportsCtrl", function ($scope, Airports) {
$scope.formURL = "views/_form.html";
$scope.currentAirport = null;
$scope.airports = Airports;
$scope.setAirport = function(code) {
$scope.currentAirport = $scope.airports[code];
};
});
同一模块中的工厂服务
angApp.factory("Airports", function() {
var Airports = {};
Airports.detail = {
"PDX": {
"code": "PDX",
"name": "Portland International Airport",
"city": "Portland",
"destinations": [
"LAX",
"SFO"
]
},
"STL": {
"code": "STL",
"name": "Lambert-St. Louis International Airport",
"city": "St. Louis",
"destinations": [
"LAX",
"MKE"
]
},
"MCI": {
"code": "MCI",
"name": "Kansas City International Airport",
"city": "Kansas City",
"destinations": [
"LAX",
"DFW"
]
}
};
return Airports;
});
HTML
<div class="container" ng-controller="AirportsCtrl">
<ul ng-repeat="airport in airports.detail">
<li><a href="" ng-click="setAirport(airport.code)">{{airport.code}}</a> -- {{airport.city}} </li>
</ul>
<p class="current"> current: {{currentAirport.name}}</p>
</div>