如何为URL参数的名称和开始日期设置条件“筛选器”?我这里有个有用的密码。问题是,如果您键入名称搜索字段" name“。开始日期也会给出一个值本身。我认为这是因为绑定URL中的"&“标记。有人对此有什么建议吗?
Name:<input type="text" class="form-control" ng-model="name" />
Start Date:<input type="text" class="form-control" ng-model="date" />
<button ng-click="search(name,date)" class="blue_button" >search</button>职能:
var myTable=angular.module('myTable',[]);
myTable.controller('tableCtrl',function($scope,$http){
$http.get("http://staging.api.sample.com/events.json", {headers: {Authorization: 'vuNYhXbpKfH73IjSw856PnGUyOAlmgTW'}})
.success(function(response) {
debugger
$scope.members=response.events;
$scope.totals = response.paging;
});
$scope.search=function(name,date){
$http.get("http://staging.api.sample.com/events.json?name="+name+"&start_date_from="+date, {headers: {Authorization: 'vuNYhXbpKfH73IjSw856PnGUyOAlmgTW'}})
.success(function(response) {
$scope.members=response.events;
$scope.totals = response.paging;
});
}
});发布于 2015-08-07 04:25:34
当您要触发请求时,可以使用字符串生成器吗?
$scope.search=function(name,date){
var requestParams = '?';
if(name) requestParams += "name= " + name;
if(name && date) requestParams += "&"
if(date) requestParams += "start_date_from=" + date;
// you may want to remove the trailing & if date is not provided
$http.get("http://staging.api.sample.com/events.json" + requestParams, {headers: {Authorization: 'vuNYhXbpKfH73IjSw856PnGUyOAlmgTW'}})
.success(function(response) {
$scope.members=response.events;
$scope.totals = response.paging;
});
}我还建议把数据调到角服务中去。
发布于 2015-08-07 04:45:25
使用提供$resource对象的服务。
angular.module('myTable')
.factory('Api', function($resource) {
var BASE_URL = 'http://staging.api.sample.com';
var events = $resource(BASE_URL + '/events.json', {}, {
get: {
method: 'GET',
cache: true,
headers: {
'Authorization': '....'
}
}
});
return {
Events: events
};
});将服务添加为依赖项,并设置可选参数。未定义值的参数将不会被设置(如果名称、日期或两者都丢失)
angular.module('myTable')
.controller('tableCtrl',function($scope,$http, Api) {
$scope.search=function(name,date) {
Api.Events.get({
'name': name,
'date': date
}).$promise.then(function(successResponse) {
//Handle success here
}, function(err) {
//Handle error here
});
};
});注意:angular.module('myTable',[])重新声明了myTable模块。请参阅AngularJS模块文档的创建与检索部分。相关双边投资条约:
请注意,使用angular.module(' myModule ',[])将创建模块myModule并覆盖任何名为myModule的现有模块。使用angular.module('myModule')检索现有模块
https://stackoverflow.com/questions/31869434
复制相似问题