Use filterFilter to filter data - angularjs

I want to filter retrived sharepoint list using filterFilter.But it is giving ***error as:**angular.min.js:89 TypeError: filterFilter is not a function*****
Not able to inject filter to controller since am using $https.
Below is the code,
$scope.update=function()
{
$scope.drop=$scope.states.Id;
console.log($scope.drop);
$http({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/getByTitle('List')/items?$select=State/Title,Title,Id&$expand=State/Title",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" }
})
.success(function (data, status, headers, config) {
$scope.city= filterFilter(data.d.results,{State:$scope.drop});
console.log($scope.city);
})
.error (function(data, status, headers, config) { })
}

Thank you for your reply ,i got my answer,Complete code is below
app.controller('Ctrl', function($filter,$scope,$http,filterFilter)
{
$http({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/getByTitle('States')/items",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" }
})
.success(function (data, status, headers, config) {
$scope.State=data.d.results;
console.log($scope.State);
})
.error (function(data, status, headers, config) { })
$scope.update=function()
{
$scope.drop=$scope.states.Id;
console.log($scope.drop);
$http({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/getByTitle('City')/items?$select=State/Title,Title,Id,*&$expand=State",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" }
})
.success(function (data, status, headers, config) {
$scope.city=filterFilter(data.d.results,{StateId:$scope.drop});
console.log($scope.city);
})
.error (function(data, status, headers, config) { })
}
});
If you use * in $select,Lookup field Id can be retrieved,using that you can compare :)

Related

Angular is waiting anoher request to start

In my App I need to do a request to save some data (this request is little slow) but the user can do another request in same time, when he did this, angular was waiting for the first request (not assync). How Can I do two requests whithout waiting for other?
My slow service:
app.service('PedSaveService',["$http", "$q","BASEURL",
function ($http, $q,BASEURL) {
var self = this;
self.Save = function (pedidoData) {
var deferred = $q.defer();
$http({method: 'POST',async: true, url: BASEURL.REST_SAVE, headers: {'Content-Type': 'application/json;charset=utf-8'}, data : pedidoData})
.success(function (response, status, headers, config) {
deferred.resolve(response, status, headers, config);
})
.error(function (response, status, headers, config) {
deferred.reject(response, status, headers, config);
});
return deferred.promise;
}
self.Save = function(pedidoData) {
$http({
method: 'POST',
async: true,
url: BASEURL.REST_SAVE,
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
data: pedidoData
}).then(function(res) {
return res;
}); }
Your code in service should be this.

Create couchdb doc using AngularJs

var post_data ={ "task_list": $scope.task_list,"uri": $scope.uri }
$http({method: 'POST',url: 'http://127.0.0.1:5984/tasklist/'+$scope.task_list, data: post_data})
.success(function(data, status, headers, config) {
console.log("POST SUCCESS")
alert("Successfully added");
})
.error(function(data, status, headers, config) {
console.log("POST ERROR", data)
})
}
} );
When I compile the above code it shows an error of
127.0.0.1:5984/tasklist/text:1 POST http://127.0.0.1:5984/tasklist/text 400 (Bad Request)
admin.html:63 POST ERROR Object {error: "bad_request", reason: "Referer header required."}
Why does it show this? Any remedy for this?
Hey you need include headers in the post data. Since your post url expecting Referer.
var post_data = {
method: 'POST',
url: 'http://127.0.0.1:5984/tasklist/'+$scope.task_list,
headers: {
'Content-Type': 'json',
'Referer': 'http://www.example.com/app/index.html' //include headers
},
data: { "task_list": $scope.task_list,"uri": $scope.uri }
}
$http(post_data).success(function(data, status, headers, config) {
console.log("POST SUCCESS")
alert("Successfully added");
})
.error(function(data, status, headers, config) {
console.log("POST ERROR", data)
});

Angular Unit Test: Error: Unexpected request: GET

How can test this type of http format below?
$http({
method: 'GET',
url: 'https://api.github.com/user/repos',
headers:{
'Authorization': "Basic " + btoa("xxxx:xxxx"),
'Accept': 'application/json; odata=verbose'
}
})
.success(function(data, status, headers, config) {
$scope.valid = true;
$scope.collection = data;
})
.error(function(data, status, headers, config) {
$scope.error = data;
});
Test code,
it('should demonstrate using when (200 status)', inject(function($http, $httpBackend) {
var $scope = {};
/* Code Under Test */
$http({
method: 'GET',
url: 'https://api.github.com/user/repos',
headers:{
'Authorization': "Basic " + btoa("xxxxx:xxxx"),
'Accept': 'application/json; odata=verbose'
}
})
.success(function(data, status, headers, config) {
$scope.valid = true;
$scope.collection = data;
})
.error(function(data, status, headers, config) {
$scope.error = data;
});
/* End */
$httpBackend
.when('GET', 'https://api.github.com/user/repos', undefined, {
Authorization: "Basic " + btoa("xxxxx:xxxx"),
Accept: "application/json;odata=verbose"
})
.respond(200, { foo: 'bar' });
$httpBackend.flush();
expect($scope.valid).toBe(true);
expect($scope.collection).toEqual({ foo: 'bar' });
}));
I get this error,
Error: Unexpected request: GET https://api.github.com/user/repos No
more request expected
Any ideas?
can you try this
$httpBackend.whenGET('https://api.github.com/user/repos', undefined, {
Authorization: "Basic " + btoa("xxxxx:xxxx"),
Accept: "application/json;odata=verbose"
}).respond(function(){ return [200,[{foo: 'bar' }]]});

ASP.NET MVC Web API Primitive Post not binding Angular

I have the following Action
[HttpPost]
[Route("api/client/log")]
public void SetClientLog([FromBody]string error)
{
ClientLogger.LogError(error);
}
And in angular i'm trying to post the following where log is just a string
$http({
method: 'POST',
url: "api/client/log",
data: log,
headers: {'Content-Type': 'application/json'}
});
Make sure you prefix your value with "=" and set the correct Content-Type:
$http.post('api/Companies/Search', "="+$scope.query, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }
})
.success(function (data, status) {
$scope.result = data;
})
.error(function (data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});

AngularJS a service is not a function

This current code when ran returns TypeError: Services.updateUserData.set is not a function
Controller
Services.updateUserData.set($rootScope.user);
Services.js
this.updateUserData = function() {
return {
set: function(data) {
console.log(data);
$http({
method: 'POST',
url: 'api/data/',
data: {'data': data},
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
}).success(function(data, status, headers, config) {
console.log(data);
}).error(function(data, status, headers, config) {
console.log(data);
});
}
}
}
The code below works but I would much rather have a get and set function within updateUserData so that I am not duplicating work.
Current Code
Controller
Services.updateUserData($rootScope.user);
Services.js
this.updateUserData = function() {
console.log(data);
$http({
method: 'POST',
url: 'api/data/',
data: {'data': data},
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
}).success(function(data, status, headers, config) {
console.log(data);
}).error(function(data, status, headers, config) {
console.log(data);
});
}
updateUserData is a function and hence there is no property called set. The option is either convert it to object
this.updateUserData={set:function(){}}
or invoke it
Services.updateUserData().set($rootScope.user);
before accessing set

Resources