2017-08-16 166 views
0

以前的問題沒有解決我的問題。ui路由器解析不起作用

我試圖發送res.data在另一個功能,它的工作原理。但是當我發送res.data[somevalue]它不起作用。基本上,我的迴應是一組對象。我想從數組中發送一個特定的對象。發送數組的作品。

Service.js

getPlayer: function(playerName){ 
    return $http.get('../data/player.json',{ 
     cache: true 
    }).then(function(res){ 
     res.data.forEach(function(element) { 
      if(element.name == playerName){ 
       console.log(element); 
       return element; 
      } 
     }); 
    }); 
} 

國家

name:'player', 
    url:'/players/{playerName}', 
    component:'player', 
    resolve:{ 
     player: function(PlayerService, $transition$){ 
      return PlayerService.getPlayer($transition$.params().playerName); 
     } 
    } 

組件

angular.module('playerApp').component('player',{ 
    bindings: {player:'<'}, 

    template: '<div class="panel panel-success">'+ 
    '<div class="panel-heading">'+ 
     '<h3 class="panel-title">Details</h3></div>'+ 
    '<div class="panel-body"><img height=300 width= 100% src="{{$ctrl.player.image}}">'+ 
    '<strong>Name:</strong> {{$ctrl.player.name}} </br>'+ 
    '<strong>Age:</strong> {{$ctrl.player.age}}</br>'+ 
    '<strong>Country:</strong> {{$ctrl.player.country}}</br>'+ 
    '<strong>Club:</strong> {{$ctrl.player.club}}</br>'+  
    '</div></div>' 
}) 
+0

你爲什麼不創建一個重擊者? – anu

+0

嘗試更改$ transition $ .params()。playerName to $ stateParams.playerName – Vivz

+0

@Vivz參數越過了,我認爲沒有問題。 –

回答

1

改變了功能getPlayers功能和它的工作。

getPlayer: function(playerName){ 
    return $http.get('../data/player.json',{ 
     cache: true 
    }).then(function(res){ 
     for(var i in res.data){ 
      if(res.data[i].name==playerName){ 
       console.log(res.data[i]); 
       return res.data[i]; 
      } 
     } 
    });