2016-09-23 2 views
-2

私のウェブアプリケーションで少し苦労していますJSONブール値が機能していないようです

初めてJSONで作業しています。この

{ 
"Code":"310254351", 
"FirstName":null, 
"LastName":null, 
"NewUser":true 
} 

は、私が「NEWUSER」ブール値が完全に伝わってくる、私のHTMLで見ることができるようにJSONに見える

。 どういうわけか、私は得ることができないが

$scope.checkForUserProfile = function() { 
    $scope.getUserProfile(); 

    if ($scope.userprofile.NewUser) 
    { 
     $scope.showWelcome = false; 
     $scope.showProfileNew = true; 
    } 
    else (!$scope.userprofile.NewUser) 
    { 
     $scope.showWelcome = true; 
    }   
} 

$scope.getUserProfile = function() { 
    $scope.loading = true; 
    $http.post('http://localhost:49165/Service1.svc/userprofile/get/').then(
     function (response) { 
      $scope.userprofile = response.data; 
     }, function (errResponse) { 
      console.error('Error while getting user profile'); 
     }); 
    $scope.loading = false; 
}; 

(角度)JSで動作するように私は、いくつかのキャスティングまたは何かが足りないのですか?

これもいくつかの方法で試しましたが、これはうまくいきません。

if ($scope.userprofile.NewUser = true){ 

//は何か }

+4

あなたのコードが構文的に正しくありません。ブラウザのデベロッパーコンソールを確認すると、エラーが表示されます。 – Pointy

+0

ここで、どのように、特に** **は '$ scope.userprofile'に実際にデータが設定されていますか? – deceze

+0

'if()'文の直前に 'console.log($ scope.userprofile)'を追加できますか? –

答えて

0

$scope.userprofileそれは未定義らしいを行います。あなたはこれをしたいはずです。

$scope.userprofile = $scope.getUserProfile(); 
+0

推測に基づいて答えるのではなく、 '$ scope.getUserProfile()'が '$ scope.userprofile'の値を実際に設定していることを明確にするためにコメントを尋ねるほうが良いかもしれません。 –

+0

よろしくお願いします。 assertivではありませんが、私にとっては明らかになっています。 –

-1
else (!$scope.userprofile.NewUser) 

elseはそのようなブール値を取ることはありません! ()の間のビットを削除します。

のでifは次のようになります。

if ($scope.userprofile.NewUser) 
    { 
     $scope.showWelcome = false; 
     $scope.showProfileNew = true; 
    } 
    else 
    { 
     $scope.showWelcome = true; 
    } 
関連する問題