使用工厂返回的数据查看绑定问题 [英] View Binding Issue Using Data Returned From Factory

查看:60
本文介绍了使用工厂返回的数据查看绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用AngularJs工厂方法来绑定带有静态数组的月份列表时,它可以正常工作.但是,当我使用MVC Controller返回相同的数据时,则$ scope.months没有绑定列表.

When I am calling AngularJs factory method to bind a list of months with static array then it is working fine. But when I am using MVC Controller to return same data then $scope.months not binding list.

尽管XHR响应具有相同的数据.我不知道是什么问题.

While the XHR response has the same data. I don't know what is the issue.

以下是代码段:

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();
}

推荐答案

使用其.then方法从诺言中提取数据:

Extract the data from the promise with its .then method:

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

    DataFactory.getMonths().then(function(data) {
        $scope.months = data;
    });
}

这篇关于使用工厂返回的数据查看绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆