Reputation: 5207
I have an AngularJS directive called product. I need an action to happen whenever I click on a button that's a part of this directive that involves making an $http.get request to my server and doing work on the response. I have a file that appears to work fine until it actually gets around to making the request, at which point, the url shown in Fiddler doesn't match what I'm attempting to make the request and I'm unsure as to why.
The request, as you can see below, should be hitting my domain/api/product/GetToken api service, but in Fiddler, I show this to be going to "/user/[object%20Object]" instead and I'm unsure as to why this is. Also, the console yields no errors.
The following is the directive in question:
angular.module('myApp').directive('product', function($location) {
return {
restrict: 'E',
replace: false,
templateUrl: 'path/to/template.html',
scope: {},
controller: function($scope, $state, $cookieStore, $http) {
$scope.productClick = function(key) {
var url = 'http://exampleurl.com/api/product/GetToken';
$http.get({url:url})
.success(d, s, h, c) {
$state.go('this.someplace');
}
.error(function(d, s, h, c) {
$state.go('this.otherview');
});
},
link: function($scope, e, a, m) {
$scope.name = a.name + "123";
}
}
}
}
Does anyone have an idea for what I can do that I'm not catching here?
Upvotes: 2
Views: 4457
Reputation: 7576
Using $http and $http.get differs somewhat. With just $http you do as you have written (send a config object), but with $http.get (or post, etc), you pass the url as a string. So it is:
$http.get('/my/url/').success(...).error(...);
or
$http.post('/my/url/', dataObject).success(...).error(...);
or with just $http
$http({url: '/my/url', method: 'GET'}).success(...).error(...);
Upvotes: 5