我想從一個API中的一些數據變量$ scope.proposition用下面的代碼範圍的數據似乎空
'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遇到一些問題。
我不知道它是什麼,因爲螢火蟲實際上告訴我,數據以及從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的範圍變量」
移動你的getProposition();之後函數getProposition() – MohamedAbbas 2014-09-01 08:33:56
我已經做到了,不改變一個東西=)謝謝! – Krowar 2014-09-01 09:52:46
console.log(callback);它會是空的嗎?如果這是真的,因爲$ http服務返回一個承諾 – MohamedAbbas 2014-09-01 10:35:34