Просмотр проблемы привязки с использованием данных, возвращаемых с завода - PullRequest
0 голосов
/ 03 марта 2019

Когда я вызываю фабричный метод 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();
}

enter image description here

1 Ответ

0 голосов
/ 03 марта 2019

Извлечение данных из обещания с помощью .then метода:

//Functions
function homeController($scope,$http, DataFactory,DataService) {
    $scope.headingText = "Home";

    DataFactory.getMonths().then(function(data) {
        $scope.months = data;
    });
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...