Passing multiple parameters from controller to factory in angularjs - javascript

I am passing three parameters from my controller to the factory following way.
In my controller I am trying to pass three parameters id,sdt and edt ..
$scope.val = function () {
$scope.tech = techRepository.getTech.query({ id: $scope.id, sdt: $scope.sDate, edt: $scope.eDate }, function(data) {
scope.tech = data;
});
};
In my factory I have
App.factory('techRepository', ['$resource', function ($resource) {
return {
getTech: $resource('/api/Tech/GetRange/:id', {id: '#id', start: '#sdt', end: '#edt'}, {query: {method: 'GET', isArray:true}})
};
}]);
When I run this I get Bad Request error. Please let me know how to pass multiple parameters. Thanks

This works fine, presuming you want :id in your query string to be replaced with the value of $scope.id, and two query parameters (sdt and edt) attached like:
http://www.example.com/api/Tech/GetRange/123?edt=20140610&sdt=20140609
It seems like you may instead be expecting a URL that looks like:
http://www.example.com/api/Tech/GetRange/123?end=20140610&start=20140609
... in which case, your code should look like:
// in controller
$scope.val = function () {
$scope.tech = techRepository.getTech.query({ id: $scope.id, start: $scope.sDate, end: $scope.eDate }, function(data) {
scope.tech = data;
});
};
.factory('techRepository', ['$resource', function ($resource) {
return {
getTech: $resource('/:id', {id: '#id'}, {query: {method: 'GET', isArray:true}})
};
}]);
Demo

Related

AngularJS - Struggling to pass value to factory

First i want to say that i'm fairly new to AngularJS, so it might be that i'm asking my question with some oddnes.
I'm trying to pass a string to a factory which in return gives me a result from my database. When i hardcode the values, everything works. But when i try to pass inn values from my view/controller things stop working.
Here is my factory:
healthServices.factory('Entry',
function ($resource) {
return $resource('http://localhost:60673/api/hierarchy/:id', { id: '#id'}, {
query: { method: 'GET', isArray: true }
});
});
Here is the controller im using:
$scope.changeData = function (type) {
$scope.entry = new Entry();
$scope.id = type;
$scope.healthData = $scope.entry.$query({ id: $scope.id });
}
And this is how it looks in my html:
<button ng-click="changeData('someValue')">
At the moment i keep getting
TypeError: value.push is not a function
As i mentioned im quite new to this, so I might be far off. Any help would be very much appreciated.
What is intended by this line of code?
$scope.entry = new Entry();
Entry is your service you want to call.
You should pass this into your controller via dependency injection.
Angular does the 'new' for you.
myApp.controller('myCntrl', HomeCtrl);
HomeCtrl.$inject = ['$scope', 'Entry'];
function HomeCtrl($scope, Entry) {
...
}
I am not seeing any wrong with your $resource configuration.
var myApp = angular.module('myApp',['ngResource']);
myApp.factory('Entry', function ($resource) {
return $resource('http://localhost:60673/api/hierarchy/:id', { id: '#id'}, {
query: { method: 'GET', isArray: true }
});
});
myApp.controller('myCtrl', ['$scope', 'Entry', function($scope, Entry){
$scope.changeData = function (type) {
$scope.entry = new Entry();
$scope.id = type;
$scope.healthData = $scope.entry.$query({ id: $scope.id });
}
}]);
i am getting below in console
GET http://localhost:60673/api/hierarchy/someValue
error lies on other part of the code, please post your controller completely.

Angular custom $update function "is not a function"

I'm trying to make an update function to replace some data on an user.
I've created the factory:
factory('Details', ['$resource', function ($resource) {
return $resource('/api/client/:id', null, {
'update': { method: 'PUT'}
});
}]);
and the controller:
.controller('ClientDetails', function ($scope, Details, $routeParams) {
$scope.client = Details.query({ id: $routeParams.id });
$scope.editClient = function () {
$scope.client.$update();
}
});
and when entering function editClient() it throws and error:
$scope.client.$update is not a function
What have I done wrong? Thanks
By default, the query method is defined to return an array of instances: 'query': {method:'GET', isArray:true}, see documentation for ng-resource. And the array does not have the $update method. From your code, you need to use the get to fetch the instance, like this:
$scope.client = Details.get({ id: $routeParams.id });
$scope.editClient = function () {
$scope.client.$update();
}

Expected response to contain an object but got an array for GET action

I'm getting this error "Error: [$resource:badcfg] Error in resource configuration for action 'get'. Expected response to contain an object but got an array"
and I don't know how to fix it. I have this service
angular.module('messages').factory('Messages', ['$resource',
function ($resource) {
return $resource('api/messages/:username', {
username: '#username'
});
}]);
and this in controller:
$scope.findOne = function () {
$scope.messages = Messages.get({
username: $routeParams.username
});
console.log($scope.messages);
};
For this route I have in API controller this
exports.read = function (req, res) {
res.json(req.message);
};
I know that I have to use $resource action isArray = true, but I don't know where to put it. I tried to do something like this:
angular.module('messages').factory('Messages', ['$resource',
function ($resource) {
return $resource('api/messages/:username', {
username: '#username'
},
{'query': {method: 'GET', isArray: true}});
}]);
but without result and still same error.
In your controller:
$scope.findOne = function () {
$scope.messages = Messages.query({
username: $routeParams.username
});
console.log($scope.messages);
};
query Instead of get, should solve it.
Use Messages.query(...) instead of method get()
What you did is correct, but you have the use the method you just created ('query'), so the call would look like this:
$scope.findOne = function () {
Messages.query({
username: $routeParams.username
}).$promise.then(function (response) {
$scope.messages = response;
console.log($scope.messages);
});
};

angularjs $resource result jsonp

I want to send a request my server via JSONP and get results but I could not do it.
My server link that returns json result.
$scope.data= [
{ id: 1, name:"D1" },
{ id: 2, name:"D2" },
{ id: 3, name:"D3" },
{ id: 4, name:"D4" }
];
I am using $resource in angularjs controller like this:
app.controller("MydataListController", function($scope, $resource, $http){
var dataquery = $resource("http://192.6.4.44/DataProvider/Search/",
{ },
{ get: {method: "JSONP"} , params:{ term: "port" } });
$scope.result = dataquery.get();
console.log($scope.result);
});
But my $scope.result retuns like this:
Resource {$promise: Object, $resolved: false, $get: function, $save:
Since $resource makes an asynchronous call, you need to pass a handler function to the get method, like so:
app.controller("MydataListController", function($scope, $resource, $http){
var dataquery = $resource("http://192.6.4.44/DataProvider/Search/",
{ },
{ get: {method: "JSONP"} , params:{ term: "port" } });
dataquery.get({}, function(data) {
$scope.result = data;
console.log($scope.result);
});
});
FYI: I haven't tested this code!

Jasmine test cases for inner function calls returning response

I am trying to write the jasmine test case for a function which in turn calls another function and returns a response. Seems like the function is called but i am not getting the value that is set inside the function.
Controller Function:
angular.module('serviceApp')
.controller('startWorkscopeCtrl', function ($rootScope, $log, $scope, CommentService) {
$scope.getTextById = function (id) {
CommentService.get({ id: id }, function (response) {
$scope.text = response;
$scope.textCount = $scope.text.length;
});
};
});
The service is referenced in another js file where it makes the rest service call to the backend service.
angular.module('restServiceModule', ['ngResource'])
.factory('CommentService', function($resource){
return $resource(
{
get: {
method: 'GET',
isArray: true,
url: window.urlPrefix + '/commentservice/:id',
params: {
id: '#id'
}
}
});
})
Test Case:
var mockTextComment;
var fakeText = ['testPass'];
beforeEach(function(){
mockTextComment = {
get: function(){
return fakeText;
}
};
});
it('should get comments by asset id', function () {
spyOn(mockTextComment, 'get');
inject(function ($controller) {
ctrl = $controller('startWorkscopeCtrl', {
$scope: scope,
CommentService: mockTextComment
});
scope.getTextById(40107);
expect(scope.textCount).toEqual(1);
});
});
With this, i am getting the below error:
**Expected undefined to equal 1.
Error: Expected undefined to equal 1.**
Not sure where i am doing wrong. Any pointers on this will be helpful.
It looks like CommentService.get does not return a value; it calls back a function upon completion. Therefore, a CommentService mock would look like this:
var mock = {
get : function(arg1, callback){
callback(fakeText);
}
}

Categories

Resources