2014-09-01 67 views
0

我想從一個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的範圍變量」

+0

移動你的getProposition();之後函數getProposition() – MohamedAbbas 2014-09-01 08:33:56

+0

我已經做到了,不改變一個東西=)謝謝! – Krowar 2014-09-01 09:52:46

+1

console.log(callback);它會是空的嗎?如果這是真的,因爲$ http服務返回一個承諾 – MohamedAbbas 2014-09-01 10:35:34

回答

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

它應該工作

+0

謝謝。 感謝您的回覆,我更新了我的新信息 – Krowar 2014-09-01 09:44:53

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

這代碼工作...

事情是,我di dn't知道$ http.get返回一個承諾,讓我忘了加上.success(callback)在該行

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

現在寄託都的作品。

感謝提示MohammedAbbas