我有當前範圍的用戶,由我當前的控制器設置。AngularJS:ng-show =「user」falsey,ng-show =「!! user」truthy?
$scope.user = user;
選擇性顯示/隱藏<div>
在此基礎上用戶的存在不工作,我所期望的,而不是一個異常被拋出。
不起作用:ng-show="user"
<div class='row' ng-show="user">
...
</div>
作品:ng-show="!!user"
<div class='row' ng-show="!!user">
...
</div>
什麼用ng-show
發生導致ng-show="user"
不是truthy?
我使用angular.1.2.6
從https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js
拋出異常:
如果我使用ng-show="user"
,然後我得到了我的控制檯以下異常:
TypeError: Cannot convert object to primitive value
at Ma (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js:14:240)
at Object.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js:185:464)
at Object.applyFunction [as fn] (<anonymous>:778:50)
at h.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js:99:211)
at h.$delegate.__proto__.$digest (<anonymous>:844:31)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js:101:256
at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js:33:182)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js:36:378
這個錯誤會消失,當我使用ng-show="!!user"
用戶對象:
這是user
對象,它是在$scope
:
{
"state": "",
"access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"token_type": "Bearer",
"expires_in": "3600",
"code": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"scope": "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.moments.write https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.profile.agerange.read https://www.googleapis.com/auth/plus.profile.language.read https://www.googleapis.com/auth/plus.circles.members.read",
"id_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"authuser": "0",
"session_state": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"prompt": "none",
"client_id": "xxxxxxxxxxx.apps.googleusercontent.com",
"g_user_cookie_policy": "single_host_origin",
"cookie_policy": "single_host_origin",
"response_type": "code token id_token gsession",
"issued_at": "1391059385",
"expires_at": "1391062985",
"_aa": "0",
"status": {
"google_logged_in": true,
"signed_in": true,
"method": "AUTO"
}
}
你能提供一個失敗的例子。似乎爲我工作。 http://jsfiddle.net/pMFjL/ – hassassin
'$ scope.user'在Google+ oauth2回調中設置,我無法在jsfiddle/plunker等上覆制 –
@hassassin - 在問題中添加了「TypeError」異常 –