问题描述
我特林得到3路数据和火力点和angularfire约束力。你可以看到我在Plunker得:
I'm tring to get 3-way data binding with firebase and angularfire. You can see what I've got in Plunker: http://plnkr.co/edit/RGA4jZK3Y6n4RkPCHK37
app.js
angular.module('ideaBattle', ["firebase"]);
服务
angular
.module('ideaBattle')
.constant('FBURL', 'https://ideabattle.firebaseio.com/')
.service('Ref', ['FBURL', Firebase])
.factory('dataBank', function(Ref, $firebase) {
return $firebase(Ref).$asArray();
});
控制器
angular
.module('ideaBattle')
.controller('ideaListCtrl', displayIdeas);
displayIdeas.$inject = ['dataBank'];
function displayIdeas(dataBank){
var vm = this;
vm.ideas = dataBank;
vm.upVote = function(idea){
vm.ideas[idea.id].votes++;
};
}
HTML
<div ng-controller="ideaListCtrl as vm">
<div ng-repeat="idea in vm.ideas | orderBy: '-votes'">
<div>
<h2>{{idea.name}}</h2>
<p>{{idea.desc|limitTo: 190}}</p>
<span class="btn" ng-click="vm.upVote(idea)">Vote! <span class="badge"> {{idea.votes}}</span></span>
</div>
</div>
</div>
Plunker版本:
它做什么,它就会从火力点的数据,并正确地显示出来,但是当我按下按钮来调用函数给予好评,只在本地更新。我知道为什么它只能在本地,但我不知道如何使它的火力也会更新。
What it does, it gets the data from firebase and displays it correctly, but when I push the button to call upVote function it only updates locally. I know why it only works locally, but I don't know how to make it also update in firebase.
我试着$ bindTo,但是从我的理解它需要$范围的工作,我想用控制器作为虚拟机的格局没有注入$范围。
I've tried with $bindTo, but from what I understand it requires $scope to work, and I'm trying to use "Controller as vm" pattern without injecting $scope.
谁能告诉我该怎么咬?
推荐答案
您可以使用AngularFire与ControllerAs语法,但ControllerAs与 $ bindTo
你不能使用它。
tl;dr; — 3-way data-binding does not work with ControllerAs syntax. The bindTo
method requires $scope
.
You can use AngularFire with ControllerAs syntax, but you can't use it with ControllerAs with $bindTo
.
$ bindTo
对 $范围
硬依赖,这将打破没有它。
$bindTo
has a hard dependency on $scope
and it will break without it.
如果你想使用AngularFire与ControllerAs语法的示例,请查看 这Plunker演示一>
If you want an example of using AngularFire with ControllerAs syntax, check out this Plunker demo.
angular.module('app', ['firebase'])
// constant for the Firebase we're using
.constant('FBURL', 'https://<your-firebase>.firebaseio.com/todos')
// return the Firebase ref as a service
.service('Ref', ['FBURL', Firebase])
// return the Todos from Firebase by returning the
// array from the factory
.factory('Todos', function(Ref, $firebase) {
return $firebase(Ref).$asArray();
})
// inject the Todos and assign them to "this"
// for the ControllerAs syntax
.controller('MainCtrl', function(Todos) {
this.todos = Todos;
});
这篇关于火力地堡3路数据和ControllerAs语法结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!