我有一個使用Firebase的簡單應用程序。對於Firebase數據庫,我保存了一些數據,保存後我想向用戶顯示警報窗口,並提供「everythig很好」或「bad」的響應。對於使用ui-bootstrap的警報窗口。對於警報我使用$ rootScope。所以,當我存了一些數據我有所回調:
person.child(someName)
.set(personData)
.then(function() {
$rootScope.alert = {type:'success', msg:'Person is added!'};
$rootScope.showAlert = true;
console.log($rootScope.showAlert, $rootScope.alert);
})
.catch(function (e) {
$rootScope.alert = {type:'danger', msg:e};
$rootScope.showAlert = true;
});
HTML是:
<div uib-alert
ng-class="'alert-' + (alert.type || 'warning')"
dismiss-on-timeout="2000"
ng-if="showAlert"
close="close()">{{alert.msg}}
</div>
<button ng-click='press()'>Press</button>
和控制器調用工廠:
$scope.press = function() {
fact.setItem({name: 'Josh', age: 20, sex: 'male'});
}
在控制檯中我看到rootScope是設定值警報和showAlert,但警報不起作用,所以我將這2個值綁定在html頁面上,並且我看到我第一次按下按鈕不起作用,但第二次正在工作等。In jsbin example I make this situation。在這種情況下我錯了?
THX的答案,它的作品!我忘了$申請和幾天不明白什麼是錯的:) – YoroDiallo