我的目標:
我想搜索會員ID的輸入成員詳細信息。
我現在還沒有到現在嗎?:
我的視圖已準備就緒。我創建了一個名爲myService的服務,它使用$ http 服務從members.json文件中獲取數據。
我的問題?
取而代之的是查找特定編號的詳細信息,我得到 所有成員的名字
想要問什麼?
誰只使用他的ID獲取搜索到的成員的詳細信息。使用搜索輸入使用AngularJS從json文件獲取詳細信息
This is my view
[1]: https://i.stack.imgur.com/PQ0BP.jpg
This is my code
[1]: https://i.stack.imgur.com/Q9tZp.jpg
This is my json file
{
"member":[
{ "Id":"1",
"Name":"Amit Singh",
"Dateofjoin":"21-03-2003",
"Number":"934234334"
},{
"Id":"2",
"Name":"Mohamad Aamir",
"Dateofjoin":"21-03-2012",
"Number":"934343434"
}
]
}
<body ng-app="myApp" ng-controller="ctrl">
<div>
<form class="form-horizontal">
<h2>Search for members.</h2>
<input type="text" name="" class='form-control' placeholder="Member ID"
ng-model="searchId"/><br>
<button class="btn btn-primary" ng-click="doSearch()">Search</button>
</form>
<div>
<p ng-repeat="x in data">
{{x.Name}}
</p>
</div>
</div>
</body>
<script type="text/javascript">
var app = angular.module("myApp",[]);
app.controller('ctrl',
['$scope','myService','$log',function($scope,myService,$log){
$scope.doSearch = function(){
myService.getData($scope.searchId).then(function(data){
$scope.data = data;
$log.info($scope.data);
});
};
}])
app.service('myService', ['$http','$log', function($http,$log){
this.getData = function(memberId){
return $http({
url:"members.json",
method:"GET",
dataType: 'json',
contentType: "application/json"
}).then(function(response){
var myData = response.data.member;
return(myData);
},function(response){
throw response;
});
}
}]);
</script>
相關的代碼必須在**這個問題,作爲文本**。不作爲圖像的網址。 –
您的服務返回'response.data.member',它是所有成員的數組。它不會對作爲參數傳遞的'memberId'做任何事情。那麼......爲什麼你認爲這個服務應該只返回一個成員,由memberId來表示呢?你爲什麼不改變你的代碼來尋找由memberId標識的成員,並返回它而不是整個數組?你需要一個循環。或者調用Array.filter()。 –
我知道它返回一個數組。我只是想知道可能的不同方法。 Thankyou btw :) –