在角度js工廠中調用ASP.NET MVC Controller Action方法時遇到麻煩。ASP.NET MVC中的Angular JS工廠5
var HomeModule = angular.module('HomeModule', []);
HomeModule.controller('HomeController', function ($scope) {
});
這是我的角JS控制器。
<div ng-app="HomeModule" class="row">
<div ng-controller="HomeController" class="row">
</div>
Index.cshtml部分代碼如下所示。 當我試圖從控制器綁定一些$ scope propetry時 - 是有效的。請幫我創建這樣的工廠來調用我的ASP.NET控制器方法。我所有的解決方案都會在控制檯中給我帶來錯
someControllerApp.factory('someFactory', ['$http', function ($http) {
var service = {
GetSomeData: function() {
return $http.post('/Some/SomeMethod');
},
};
return service;
}]);
錯誤:
angular.js:14324 TypeError: someFactory.GetSomeData(...).success is not a function
at ChildScope.$scope.GetData (HomeController.js:7)
at fn (eval at compile (angular.js:1), <anonymous>:4:141)
at callback (angular.js:26673)
at ChildScope.$eval (angular.js:17958)
at ChildScope.$apply (angular.js:18058)
at HTMLButtonElement.<anonymous> (angular.js:26678)
at defaultHandlerWrapper (angular.js:3613)
at HTMLButtonElement.eventHandler (angular.js:3601)
當控制器:
var HomeModule = angular.module('HomeModule', []);
HomeModule.controller('HomeController', function ($scope, someFactory) {
$scope.GetData = function() {
someFactory.GetSomeData().success(function (data) {
if (data.isSuccess) {
$scope.Address = data;
} else {
window.location.assign("/Error/Error500");
}
});
};
});
HomeModule.factory('someFactory', ['$http', function ($http) {
var service = {
GetSomeData: function() {
return $http.post('/Home/GetUserData');
}
};
return service;
}]);
什麼是在控制檯中的錯誤? – KieranDotCo
編輯後出現錯誤 –