FirebaseObjectObservable 的更新方法交替更新 DOM 和 Firebase 数据库
Update method for the FirebaseObjectObservable alternatively updates DOM and the Firebase Database
FirebbaseObjectObservable 的更新方法交替更新 DOM 和 Firebase 数据库。
ngOnInit() {
//Get Posts from posts service
this.subscription = this.service.getCandidates()
.subscribe(response => {
this.candidates = response;
})
}
vote(candidate){
this.service.vote(candidate)
.update({votes: candidate.votes++});
}
ngOnDestroy(){
this.subscription.unsubscribe();
}
服务来源
export class PollService {
constructor(private http: Http, private db: AngularFireDatabase) {
}
getCandidates(){
//Get Candidates from Firebase and Return an observable
return this.db.list('/candidates');
}
vote(candidate) {
//Update the no. of votes corresponding to the candidate id
return this.db.object('/candidates/' + candidate.$key)
}
}
每当我第一次调用投票函数时,它会更新 DOM 但实际上不会更新数据库,下次我调用此函数时,它会更新数据库但不会更新 DOM。
我正在使用 Angular 5.2.0、Firebase 4.2.0。这是我的 package.json 文件
{
"name": "random-proj",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"angular-crypto-js": "^1.0.7",
"angularfire2": "^4.0.0-rc.1",
"bootstrap": "^4.1.1",
"core-js": "^2.4.1",
"firebase": "^4.2.0",
"rxjs": "^5.5.6",
"sha.js": "^2.4.11",
"zone.js": "^0.8.19"
},
"devDependencies": {
"@angular/cli": "1.6.6",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
}
}
(发现问题,想把它放在这里以防其他人将来遇到类似的问题)
将 candidate.votes++
替换为 ++candidate.votes
使其按预期工作。在变量后面加上++,先把相同的数存入数据库,然后增加candidate.votes
的本地值
FirebbaseObjectObservable 的更新方法交替更新 DOM 和 Firebase 数据库。
ngOnInit() {
//Get Posts from posts service
this.subscription = this.service.getCandidates()
.subscribe(response => {
this.candidates = response;
})
}
vote(candidate){
this.service.vote(candidate)
.update({votes: candidate.votes++});
}
ngOnDestroy(){
this.subscription.unsubscribe();
}
服务来源
export class PollService {
constructor(private http: Http, private db: AngularFireDatabase) {
}
getCandidates(){
//Get Candidates from Firebase and Return an observable
return this.db.list('/candidates');
}
vote(candidate) {
//Update the no. of votes corresponding to the candidate id
return this.db.object('/candidates/' + candidate.$key)
}
}
每当我第一次调用投票函数时,它会更新 DOM 但实际上不会更新数据库,下次我调用此函数时,它会更新数据库但不会更新 DOM。
我正在使用 Angular 5.2.0、Firebase 4.2.0。这是我的 package.json 文件
{
"name": "random-proj",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"angular-crypto-js": "^1.0.7",
"angularfire2": "^4.0.0-rc.1",
"bootstrap": "^4.1.1",
"core-js": "^2.4.1",
"firebase": "^4.2.0",
"rxjs": "^5.5.6",
"sha.js": "^2.4.11",
"zone.js": "^0.8.19"
},
"devDependencies": {
"@angular/cli": "1.6.6",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
}
}
(发现问题,想把它放在这里以防其他人将来遇到类似的问题)
将 candidate.votes++
替换为 ++candidate.votes
使其按预期工作。在变量后面加上++,先把相同的数存入数据库,然后增加candidate.votes