1

我試圖使用角resource.js文件在我的演示。我是從JSON文件中使用$ 資源檢索 數據,並使用NG-重複。但每一個項目我增加了一個按鈕,可以顯示我的數據(信息文本按鈕)。我需要得到它信息來源使用$資源 property.I我上點擊功能發送ID。但是,當我使用$資源屬性提示錯誤我如何通過在角js中使用ID來獲取數據?

$scope.getIn=function(id){ 
    // alert(id) 
     $scope.oneUser = Entry.get({user: id}); 
     console.log($scope.oneUser) 
    } 

這裏是我的代碼 http://plnkr.co/edit/lB11oQkQjbILK36u8V25?p=preview

+0

好,'$ resource'是指具有REST API,但你的工作正試圖從JSON文件中獲取某些內容。 – Puigcerber

回答

1

如果我正確理解你想達到什麼目的這應該解決這個問題:

angular.module('app',['ngResource']).controller('a',function($scope, GetData){ 
    // read data from factory and store it in $scope.posts 
    GetData.then(
     function successCallback(data) { 
      $scope.posts = data.data; 
     }, 
     function errorCallback(response) { 
      console.log(response); // handle any errors 
     }); 

    // buttonclick to return the values from the selected id 
     $scope.getIn = function(id){ 
     angular.forEach($scope.posts, function(value) { 
      if(value.id === id) 
      { 
      $scope.selectedValue = value; 
      } 
     }) 
     }; 

    console.log($scope.posts) 
    }) 
    .factory('GetData', function($http){ // use http to read the json 
    return $http.get('data.json'); 
    }); 

和HTML:

<body ng-app='app' ng-controller='a'> 
    <div ng-repeat='p in posts'> 
     <span>{{p.name}}</span> 
     <button ng-click=getIn(p.id)>info</button> 
    </div> 
    {{selectedValue}} 
    </body>