加入数组元素另一个数组 [英] adding array elements to another array

查看:178
本文介绍了加入数组元素另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的列表,它是一个名为数组联赛,我需要让用户采取数组(列表)上的元素,并选择那些为通过点击一个按钮的最爱

  $ scope.favoriteLeagues = [];$ scope.favoriteLeague =功能(联赛){  $ scope.favoriteLeagues.push(联盟);}

所以我想知道我究竟做错了什么?功能有时让我加一为收藏,但一旦我点击第二个,我得到了一些不确定的消息,而且,不工作的结合,我无法看到 { {} favoriteLeagues.name} 打印。

已更新的要求

 < D​​IV>
      <强>的最爱LT; / STRONG>
        {{favoriteLeagues.name}}
    < / DIV>
    <离子选项按钮类=按钮点亮图标离子明星
                       上自来水=favoriteLeague(联盟)>
    < /离子选项按钮>
    < D​​IV NG重复=在sportsFilter =运动(运动|过滤器:查询)>
            <强> {{sport.name}}< / STRONG>
     < / DIV>
          <离子项目NG重复=联赛sport.leagues>
            < D​​IV> {{league.name}}< / DIV>
          < /离子项目>
        < /离子列表>

在这里控制器:

  .controller('SportsController',函数($范围,$状态
                                           AuthFactory,SportsFactory){
    $ scope.favoriteLeagues = [];
    $ scope.sports = [];    AuthFactory.getCustomer()。然后(函数(客户){
      $ scope.customer =客户;
      SportsFactory.getSportsWithLeagues(客户)。然后(函数(体育){
        如果(sports.length){
          $ scope.sports =运动;
        }      $ scope.isSportShown =功能(运动){
        返回$ scope.shownSport ===运动;
      };      $ scope.favoriteLeague =功能(联赛){        $ scope.favoriteLeagues.push(联盟);       }      };     });


解决方案

您还没有粘贴完整的HTML,但它应该是这个样子:

 <! - 使用NG-应用自动引导的AngularJS应用程序 - >
<! - 使用NG控制器与您SportsController控制器接到你的观点 - >
<离子列表>
    < D​​IV>
        <强>的最爱LT; / STRONG>
        <! - 在所有的喜爱联赛循环 - >
        < D​​IV NG重复=favouriteL在favoriteLeagues>
            {{favouriteL.name}}
        < / DIV>
    < / DIV>    <! - 在所有的运动循环 - >
    < D​​IV NG重复=在sportsFilter =运动(运动|过滤器:查询)>
        <! - 绑定这项运动的名字 - >
        <强> {{sport.name}}< / STRONG>        <! - 通过所有联盟循环 - >
        <离子项目NG重复=联赛sport.leagues>
            <! - 显示一个按钮,自来水将调用favoriteLeague功能 - >
            <离子选项按钮类=按钮点亮图标离子明星上自来水=favoriteLeague(联盟)>
            < /离子选项按钮>
            <! - 绑定联赛的名字 - >
            < D​​IV> {{league.name}}< / DIV>
        < /离子项目>    < / DIV>
< /离子列表>

不要忘了用NG-控制器,控制器视图连接。

I have a very big list which is an array named leagues, I need to allow the user to take the elements on that array(list) , and choose those as favorites by clicking a button

$scope.favoriteLeagues = [];

$scope.favoriteLeague = function(league) {

  $scope.favoriteLeagues.push(league);

}

so I want to know what am I doing wrong ? the function sometimes allows me to add one as favorite, but once I click on the second one, I got a message of something undefined, and also, the binding is not working, I am unable to see the {{favoriteLeagues.name}} printed.

UPDATED AS REQUESTED

<div>
      <strong>Favorites</strong>
        {{favoriteLeagues.name}}
    </div>
    <ion-option-button class="button-light icon ion-star"
                       on-tap="favoriteLeague(league)">
    </ion-option-button>
    <div ng-repeat="sport in sportsFilter = (sports | filter:query)">
            <strong>{{sport.name}}</strong>
     </div>
          <ion-item ng-repeat="league in sport.leagues">
            <div>{{league.name}}</div>
          </ion-item>
        </ion-list>

here the controller:

  .controller('SportsController', function($scope, $state,
                                           AuthFactory, SportsFactory) {
    $scope.favoriteLeagues = [];
    $scope.sports = [];

    AuthFactory.getCustomer().then(function(customer) {
      $scope.customer = customer;
      SportsFactory.getSportsWithLeagues(customer).then(function(sports) {
        if (sports.length) {
          $scope.sports = sports;
        }

      $scope.isSportShown = function(sport) {
        return $scope.shownSport === sport;
      };

      $scope.favoriteLeague = function(league) {

        $scope.favoriteLeagues.push(league);

       }

      };

     });

解决方案

You haven't pasted the full html, but it should look something like this:

<!-- Use ng-app to auto-bootstrap an AngularJS application-->
<!-- Use ng-controller to attach your view with your SportsController  controller -->
<ion-list>
    <div>
        <strong>Favorites</strong>
        <!-- Looping through all the favourite leagues-->
        <div ng-repeat="favouriteL in favoriteLeagues">
            {{favouriteL.name}}        
        </div>
    </div>

    <!-- Looping through all the sports -->            
    <div ng-repeat="sport in sportsFilter = (sports | filter:query)"> 
        <!-- Bind the sport name -->            
        <strong>{{sport.name}}</strong>

        <!-- Looping through all the leagues -->                
        <ion-item ng-repeat="league in sport.leagues">
            <!-- Display a button which on tap will call favoriteLeague function -->                
            <ion-option-button class="button-light icon ion-star" on-tap="favoriteLeague(league)">    
            </ion-option-button>
            <!-- Bind the name of the league -->                
            <div>{{league.name}}</div>
        </ion-item>

    </div>
</ion-list>

Don't forget to attach the view with your controller using ng-controller.

这篇关于加入数组元素另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆