【发布时间】:2019-03-03 18:44:14
【问题描述】:
当我调用 AngularJs 工厂方法将月份列表与静态数组绑定时,它工作正常。但是当我使用 MVC 控制器返回相同的数据时,$scope.months 不是绑定列表。
虽然 XHR 响应具有相同的数据。我不知道是什么问题。
下面是代码sn-p:
HomeController.css
[HttpGet]
public ActionResult GetAllMonths()
{
List<Month> monthList = new JsonRepository<Month>().GetAll();
var output = Json(monthList, JsonRequestBehavior.AllowGet);
return output;
}
AngularJs 工厂
(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();
}
【问题讨论】:
标签: angularjs model-view-controller