当我调用AngularJs工厂方法来绑定带有静态数组的月份列表时,它可以正常工作。但是,当我使用MVC Controller返回相同的数据时,则$ scope.months没有绑定列表。
尽管XHR响应具有相同的数据。我不知道是什么问题。
以下是代码段:
HomeController.css
[HttpGet]
public ActionResult GetAllMonths()
{
List<Month> monthList = new JsonRepository<Month>().GetAll();
var output = Json(monthList, JsonRequestBehavior.AllowGet);
return output;
}
AngularJs Factory
(function () {
'use strict'
var app = angular.module("CommonFactory", []);
app.factory("DataFactory", ["$http",DataFactory]);
/*DI For Factory*/
//DataFactory.$inject = ["$http"];
//Factories callBack functions
function DataFactory($http) {
return {
getMonths: function () {
return $http({
method: 'GET',
url: '/Home/GetAllMonths'
}).then(function(response){
return response.data;
});
}
}
}
})();
AngularJs控制器
//Modules With Controllers
var app = angular.module("PublicModule", [])
.controller("HomeController", homeController);
//Dependency Injection
homeController.$inject = ["$scope","$http", "DataFactory", "DataService"];
//Functions
function homeController($scope,$http, DataFactory,DataServic) {
$scope.headingText = "Home";
$scope.months = DataFactory.getMonths();
}
答案 0 :(得分:1)
使用.then
方法从承诺中提取数据:
//Functions
function homeController($scope,$http, DataFactory,DataService) {
$scope.headingText = "Home";
DataFactory.getMonths().then(function(data) {
$scope.months = data;
});
}