首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在ng上设置和显示当前数据?

在ng上设置和显示当前数据?
EN

Stack Overflow用户
提问于 2013-12-31 23:24:46
回答 1查看 166关注 0票数 0

我用的是约曼角发生器。

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中单击机场链接?

控制器

代码语言:javascript
运行
复制
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];
    };

  });

工厂服务在同一个模块

代码语言:javascript
运行
复制
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;
});

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-01 00:30:47

您的setAirport函数应该编写为:

代码语言:javascript
运行
复制
$scope.setAirport = function (code) {
  $scope.currentAirport = $scope.airports.detail[code];
};

但是,可以通过直接传递实际的airport对象来简化这一点:

代码语言:javascript
运行
复制
<a href ng-click="setAirport(airport)">{{airport.code}}</a>

$scope.setAirport = function (airport) {
  $scope.currentAirport = airport;
};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20864187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档