我用的是约曼角发生器。
JSBin:JSBin链路
我有一个从工厂angApp.factory("Airports", function() {});设置并从ng重复<ul ng-repeat="airport in airports.detail">显示的机场的简单列表。

我希望有一个交互,当每个链接被点击,它将匹配机场代码,并显示在一个新的段落标签<p class="current">Current: {{currentAirport.name}}</p>。
为什么setAirport(airport.code)函数不设置currentAirport.name(或显示?)何时在html中单击机场链接?
控制器
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;
});<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>发布于 2014-01-01 00:30:47
您的setAirport函数应该编写为:
$scope.setAirport = function (code) {
  $scope.currentAirport = $scope.airports.detail[code];
};但是,可以通过直接传递实际的airport对象来简化这一点:
<a href ng-click="setAirport(airport)">{{airport.code}}</a>
$scope.setAirport = function (airport) {
  $scope.currentAirport = airport;
};https://stackoverflow.com/questions/20864187
复制相似问题