将json放入其中后,范围数据似乎是空的

时间:2014-09-01 08:24:34

标签: asp.net json angularjs

我正在尝试使用以下代码从变量$ scope.proposition中的API获取一些数据

'use strict';

var app = angular.module('app', []);

app.controller('propositionCtrl',['$scope', 'propositionRepository',
    function ($scope, propositionRepository) {
       // $scope.proposition = { marge_propose: 3 };
        function getProposition() {
            propositionRepository.getProposition(function (result) {
                $scope.proposition = result;
                console.log("we put the json in the scope variable");
            }
            )
        }
        getProposition();
        $scope.proposition = {marge_propose : 3};
    }])

app.factory('propositionRepository', function ($http) {
    return {
        getProposition: function (callback) {
            $http.get('/api/propositionapi/3');
            console.log("we call the api");
        }
    }
})

使用以下视图

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div ng-controller="propositionCtrl">

    <p>{{ proposition.marge_propose}}</p>


</div>

它不起作用,但当我取消注释$scope.proposition = { marge_propose: 3 };时,一切正常。

这就是为什么我认为是我的getProposition遇到了一些问题。

我不知道它是什么,因为firebug实际上告诉我数据很好地从api接收到以下结果

{"$id":"1","t_concours":{"$id":"2","t_proposition":[{"$ref":"1"}],"id":1,"intitule":"test","valeur":10.0},"t_demandeur":{"$id":"3","t_proposition":[{"$ref":"1"}],"id":"1","nom":"dylan","prenom":"bob","note":"A"},"t_index":{"$id":"4","t_proposition":[{"$ref":"1"}],"id":1,"intitule":"test","valeur":10.0},"t_proposition_concurence":null,"id":3,"id_demandeur":"1","date_proposition":"1991-07-05T00:00:00","type":true,"point_de_vente":"01","code":"a","code_sas":"846684","montant_operation_hors_frais":200,"concours_sollicite":10,"duree_concours":10,"type_concours":1,"id_index":1,"apport_personnel":10,"garantie_reelle":true,"in_fine":false,"majoration":1.0,"prospect":true,"primo_accedant":true,"motif_montant":false,"motif_contrepartie":false,"motif_depot_cmne":false,"id_prop_concurence":null,"motif_autre":true,"motif_commentaire":"true","proposition_derogatoire":true,"visa_responsable":"false","marge_grille":5.0,"marge_theorique":7.0,"marge_propose":32.0}

有人可以解释我的问题所在吗?

非常感谢!

编辑:

控制台日志显示“我们调用api”但不是“我们将json放在范围变量中”

2 个答案:

答案 0 :(得分:0)

您可以尝试将额外数据设置为真正的JSON,例如:

$scope.proposition = { "marge_propose": "3" };

也许在你的propositionRepository中尝试console.log来查看你得到的结果,在getProposition()中放置一个console.log来检查一切是否正在通过

编辑: 这是一个工作小提琴: http://jsfiddle.net/c1zxp6uo/

底线:你不打电话给回叫,所以当你这样做时

$http.get('/api/propositionapi/3').then(function(){callback()})

它应该有用

答案 1 :(得分:0)

'use strict';

var app = angular.module('app', []);

app.controller('propositionCtrl',['$scope', 'propositionRepository',
    function ($scope, propositionRepository) {
       // $scope.proposition = { marge_propose: 3 };
        function getProposition() {
            propositionRepository.getProposition(function (result) {
                console.log("we put the json in the scope variable");
                $scope.proposition = result;

            }
            )
        }
        getProposition();

        $scope.proposition = {marge_propose : 3};
    }])

app.factory('propositionRepository', function ($http) {
    return {
        getProposition: function (callback) {
            $http.get('/api/propositionapi/3').success(callback);
            console.log("we call the api");
        }
    }
})

此代码有效......

问题是我不知道$ http.get会返回一个承诺,所以我忘了在行中添加.success(callback)

$http.get('/api/propositionapi/3').success(callback);

现在开始工作。

感谢MohammedAbbas提示