我有json和休息电话。如何在angular js中编写searchController。能帮帮我吗。谢谢你。
.html()
Json数据: -
url :- `http://local.agon.org/api/members/search/raju`
方法: [
{
"id": 3,
"name": "Raju",
"phone": 1597536841,
"photo": null,
"skill_level": "intermediate",
"rfid": "45128",
"created_at": "2016-10-20 14:39:32",
"updated_at": "2016-10-20 14:39:32"
}
]
搜索按钮的html代码: -
GET
答案 0 :(得分:1)
最好在工厂保留服务电话,例如:
(function() {
'use strict';
angular
.module('app')
.factory('activityApi', activityApi);
activityApi.$inject = ['$http'];
function activityApi($http) {
var service = {
getActivityById : getActivityById ,
};
return service;
function getActivityById (data) {
$http({
method: 'GET',
url: 'http://local.agon.org/api/members/search/raju',
headers: {
"Content-Type": "text/plain",
},
data: data,
}).then(function(reply) {
return reply;
})
};
};
})();
<强>控制器强>
(function() {
'use strict';
angular
.module('app')
.controller('searchController', searchController);
searchController.$inject = ['activityApi','$scope'];
function searchController(activityApi, $scope) {
$scope.getUser = function(){
var data = {"id":3,
"name":"Raju",
"phone":1597536841,
"photo":null,
"skill_level":"intermediate",
"rfid":"45128",
"created_at":"2016-10-20 14:39:32",
"updated_at":"2016-10-20 14:39:32"}
activityApi.getActivityById(data)
.then(function(result){
console.log(result)
})
}
}
})();
如果你想在控制器中进行http调用,只需注入$http
:
(function() {
'use strict';
angular
.module('app')
.controller('searchController', searchController);
searchController.$inject = ['$http','$scope'];
function searchController( $http, $scope) {
$scope.getUser = function(){
var data = {"id":3,
"name":"Raju",
"phone":1597536841,
"photo":null,
"skill_level":"intermediate",
"rfid":"45128",
"created_at":"2016-10-20 14:39:32",
"updated_at":"2016-10-20 14:39:32"}
}
$http({
method: 'GET',
url: 'http://local.agon.org/api/members/search/raju',
headers: {
"Content-Type": "text/plain",
},
data: data,
}).then(function(reply) {
console.log(reply);
},function(err){
console.log(err);
});
}
})();