Reputation: 25
I want to make simple String webApi get request by angularjs
I do not know what is the problem
Is there any better way to get simple string from webApi?
This the error
angular.min.js:2TypeError: currentYear.thisYear(...).success is not a function
web api
// GET: api/values
[HttpGet]
public string Get()
{
string time = DateTime.Now.Year.ToString();
return time;
}
Service:
(function () {
'use strict';
var timeService = angular.module('timeService', ['ngResource']);
timeService.factory('currentYear', ['$http', function ($http) {
return {
thisYear: function () {
return $http.get('/api/year/');
}
};
}]);
})();
Controller
(function () {
'use strict';
angular.module('saniehha').controller('currentYearController', currentYearController);
currentYearController.$inject = ['$scope','currentYear'];
function currentYearController($scope, currentYear) {
var value = currentYear.thisYear().success(function (data) {
alert(data);
});
$scope.currentYear = value;
}
})();
Upvotes: 2
Views: 218
Reputation: 136134
As of Angular 1.6, the .success
and .error
callback functions are removed from the API. They were deprecated in at least 1.4.
You should use .then
instead of .success
, as $http
methods return a promise
object:
var value= currentYear.thisYear().then(function (data) {
$scope.currentYear = data.data;
});
You could use that to chain it further.
$http's deprecated custom callback methods - success() and error() Changelog
Upvotes: 1