【发布时间】:2013-12-31 23:24:46
【问题描述】:
我正在使用 Yeoman - 角度生成器。
JSBin:JSBin Link
我有一个从工厂 angApp.factory("Airports", function() {}); 设置并从 ng-repeat <ul ng-repeat="airport in airports.detail"> 显示的机场的简单列表。
我希望有一个交互,当点击每个链接时,它将匹配机场代码并将其显示在新的段落标签<p class="current">Current: {{currentAirport.name}}</p>中。
为什么在html中点击机场链接时setAirport(airport.code)函数不会设置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>
【问题讨论】:
-
你能设置一个显示这个[不]工作的小提琴吗?
-
是的,我会尝试的。我非常不擅长构建 Angular 应用程序(使用 yeoman 来获得我的限制的一种作弊)
-
一切就绪@drew_w 感谢您的帮助
-
没问题,很高兴我能帮上忙!
-
您何时可以发布答案,我正在积极等待将其标记为正确。或者,如果您还有其他问题,我正在等待
标签: javascript html angularjs yeoman angularjs-ng-click