我想从一个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