2016-12-05 80 views
0

我试图做的是,当单击图像时,我发送一个包含信息的数组放入函数'test',将它与来自另一个数据库的值进行比较,然后我试图返回正确的信息并显示它。代码:单击图像,然后显示来自数据库的信息

<div data-ng-repeat="hero in heroes | filter:search | orderBy:'id'"> 
    <div class="wrap"> 
    <img class="img-rounded" ng-src="images/heroes/{{hero.url}}_full.png" ng-click="test(hero.abilities)"> 
    <div class="tooltip"> 
     <p class="fat">{{hero.localizedName}}</p> 
    </div> 
    </div> 
</div>      
<div data-ng-repeat="ability in test| orderBy:'id'"> 
    {{ability.localizedName}} //Should show the information of the selected hero 
</div> 

和脚本,

$scope.test = function(text){ 
      var newSize = text.length; 
      var temp = []; 

      for(i = 0; i < newSize; i++) 
      { 
       for(j = 0; j < $scope.abilities.length; j++) 
       { 
        if($scope.abilities[j].id == text[i]) 
        { 
        temp[i] = $scope.abilities[j]; 
        } 
       } 
      } 
      //$scope.test = temp; 
      console.log(temp); 
     } 

当我控制台记录了温度,我得到我想要的价值,但我不能获取值从机能恢复。

控制台只给我的错误消息说

“测试”未声明

,我已经搜索该错误消息,但我似乎并没有被能够找到我正在寻找的答案。

在此先感谢!

+0

为$ scope.test在同一个控制器作为$ scope.heros? –

+0

$ scope.heroes位于同一个控制器中,是的! –

+0

你可以发布你的完整控制器吗? –

回答

0

你试图重复test

<div data-ng-repeat="ability in test| orderBy:'id'"> 

test是一个功能可按。

test()函数结束,保存temp$scope,例如:

$scope.arr = temp; 

和更改模板重复arr

<div data-ng-repeat="ability in arr| orderBy:'id'"> 
0

完整的控制器(相关代码):

function AppController($filter, $log, $scope, dotaService, abilitiesService, itemsService) 
     (..) 
    $scope.test = function(text){ 
       var newSize = text.length; 
       var temp = []; 

       for(i = 0; i < newSize; i++) 
       { 
        for(j = 0; j < $scope.abilities.length; j++) 
        { 
         if($scope.abilities[j].id == text[i]) 
         { 
         temp[i] = $scope.abilities[j]; 
         } 
        } 
       } 
       //$scope.test = temp; 
       console.log(temp); 

      //Heroes 
      var promise = dotaService.getHeroes(); 
      promise.then(function (data) 
      { 
       $scope.heroes = data.data; 
      }) 
      //Abilites 
      var promise2 = abilitiesService.getAbilities(); 
      promise2.then(function (data) 
      { 
       $scope.abilities = data.data; 
      });   
     } 
相关问题