2014-09-01 54 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