2017-06-15 7 views
2

私はちょうどAngularを使い始めて、$ http(取得)を使って返されている単一のレコードを表示する問題を持っています。私はデータを正しく戻しています。これは...角度表示jsonの単一のレコード

Name: {{udCtrl.name}} 

私が持っている....

<div ng-controller="UserDataController as udCtrl"> 
    Name: {{udCtrl.user.name}} 
</div> 

<div id="debug" style="margin-top:24px;border:solid 1px #900;background:#efefef;min-height:100px"></div> 

も試み、他のバリエーションのカップルしているhtmlですJavascriptを:

(function() { 

var app = angular.module('rtcTimesheet', []); 
var servicePath="/angular/"; 
$("#debug").append("Starting...<br/>"); 

app.controller("UserDataController",["$http",function($http){ 

    var user=this; 
    user=[]; 

    $http({ 
     method:  'GET', 
     url:  servicePath+'login.php', 
     params: { 
      un:  "username", 
      pwd: "123456789" 
     } 
    }).then(function(response){ 

     if(response.data.hasOwnProperty("HasError")) { 
      $("#debug").append("ERROR: " + response.data.ErrorMessage); 
     } else { 
      $("#debug").append("Data: " + JSON.stringify(response.data)); 
      user=response.data; 
     } 

    },function (err){ 
     alert("ERROR: "+err.status); //data, status, headers, config, statusText 
    }); 

}]); 

app.controller("UserTest",function(){ 
    this.user=users; 
}); 

var users = { 
       id: '1', 
       name: 'Joe Bloggs' 
       }; 

})(); 

これは何ですかJSON形式で返され、私が作成した小さなデバッグ領域でこれを見ることができます。

{"data":{"id":"1","name":"Joe Bloggs"} 

以下のコードを使用するようにhtmlを変更すると動作します。

どこが間違っているのか、返された名前が表示されないのはわかりません。

+0

は 'JSON.stringify'を試しので、'ユーザは= JSON.stringify(response.data)? –

答えて

0

$scopeuser変数を定義し、$scope.userでアクセスしてください。あなたは相談に問題があります。

//Define user variable like this. 
$scope.user = {}; 

//On response --> 
}).then(function(response){ 

    if(response.data.hasOwnProperty("HasError")) { 
     $("#debug").append("ERROR: " + response.data.ErrorMessage); 
    } else { 
     $("#debug").append("Data: " + JSON.stringify(response.data)); 
     $scope.user=response.data; 
    } 

} 

EDIT

あなたは、コントローラ上のthis変数の下に変数を定義するudCtrl referanceを使用する場合の例。 `` user`に割り当てる場合

//Define user variable like this. 
var ctrl = this; 
ctrl.user = {}; 

//On response --> 
}).then(function(response){ 

    if(response.data.hasOwnProperty("HasError")) { 
     $("#debug").append("ERROR: " + response.data.ErrorMessage); 
    } else { 
     $("#debug").append("Data: " + JSON.stringify(response.data)); 
     ctrl.user=response.data; 
    } 

} 

ABSOLUTE ANSWER FOR EDIT 2

(function() { 

var app = angular.module('rtcTimesheet', []); 
var servicePath="/angular/"; 
$("#debug").append("Starting...<br/>"); 

app.controller("UserDataController",["$http",function($http){ 

    var udCtrl=this; 
    udCtrl.user=[]; 

    $http({ 
     method:  'GET', 
     url:  servicePath+'login.php', 
     params: { 
      un:  "username", 
      pwd: "123456789" 
     } 
    }).then(function(response){ 

     if(response.data.hasOwnProperty("HasError")) { 
      $("#debug").append("ERROR: " + response.data.ErrorMessage); 
     } else { 
      $("#debug").append("Data: " + JSON.stringify(response.data)); 
      udCtrl.user=response.data; 
     } 

    },function (err){ 
     alert("ERROR: "+err.status); //data, status, headers, config, statusText 
    }); 

}]); 

app.controller("UserTest",function(){ 
    this.user=users; 
}); 

var users = { 
       id: '1', 
       name: 'Joe Bloggs' 
       }; 

})(); 
+0

多くのありがとう@BurakAkyildiz、ちょうど私のコードをvar udCtrl = thisに変更しました。 \t \t udCtrl.user = []; となりました。あなたが時間があれば、私が間違っていた場所を説明することができました。非常に新しいAngularで、コードスクールの教訓に従っています... – Martin

+0

@Martinはあなたのコードがあります。 –

関連する問題