当数据库发生变化时,如何以角度更新视图?

时间:2015-09-03 21:51:14

标签: angularjs firebase

我在firebase中使用了角度和2个数据库的SPA。 所以这是一个控件(有2个选项),我想选择数据库来获取数据。例如,第一选项表示第一数据库,第二选项表示第二选项。 问题是:“当数据库发生变化时,如何以角度更新视图”。

<select class="form-control" ng-model="level" ng-options="lvl in lvls"></select>
<div>{{data}}</div>
控制器中的

var levelString = "https://amber-inferno-9289.firebaseio.com/" + $scope.level + "/";

$scope.dictionary = new Firebase(levelString);
$scope.dictionary.on('value', function(snapshot){
    $scope.data = snapshot.val();
}, function (errorObject) {
    console.log("The read failed: " + errorObject.code);
});

1 个答案:

答案 0 :(得分:0)

我认为您要做的就是在ng-change上使用select指令,并将您的字典设置逻辑重构为一个在选择更改时调用的函数。

<select class="form-control" ng-model="level" ng-options="lvl for lvl in lvls" ng-change="setUpDictionary(level)"></select>

然后在您的控制器中,创建setUpDictionary功能,将$scope.dictionary设置为指向不同网址等的新Firebase。

我认为这是您根据我的问题所能提供的内容。

希望它可以帮助你!