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>