【问题标题】:AngularFire 3-way-binding without $scopeAngularFire 3-way-binding 没有 $scope
【发布时间】:2015-04-03 05:59:41
【问题描述】:

所以我正在阅读关于 3-way-binding 的 AngularFire 文档,并尝试在没有 $scope 的情况下进行操作。这是我的控制器:

angular.module('APP', ['firebase']).controller('Ctrl', Ctrl);

    Ctrl.$inject = ['$firebaseObject'];

    function Ctrl($firebaseObject) {
        var vm = this;

        var ref = new Firebase('https://testfirebase23.firebaseio.com/');
        var syncObj = $firebaseObject(ref);

        syncObj.$bindTo(vm, 'data');
    }

当我运行代码时出现此错误:

TypeError: undefined is not a function
    at e (angularfire.min.js:12)
    at Object.e.bindTo (angularfire.min.js:12)
    at angularfire.min.js:12
    at processQueue (angular.js:13248)
    at angular.js:13264
    at Scope.$eval (angular.js:14466)
    at Scope.$digest (angular.js:14282)
    at Scope.$apply (angular.js:14571)
    at angular.js:16308
    at completeOutstandingRequest (angular.js:4924)

我不明白这个错误。有没有办法在没有 $scope 的情况下将对象或数组三向绑定到控制器?

【问题讨论】:

  • 尝试在非缩小模式下运行,看看错误是否仍然存在。
  • 我没有缩小任何东西。

标签: javascript angularjs data-binding angularfire


【解决方案1】:

$bindTo of firebase 永远不会使用 controllerAs 语法, 您可以通过将 firebase 对象直接分配给任何控制器上下文变量来实现此目的。

代码

var ref = new Firebase('https://testfirebase23.firebaseio.com/');
vm.syncObj = $firebaseObject(ref); //this will ensure your binding will work

请参考this SO answer

【讨论】:

  • var ref = new Firebase('https://testfirebase23.firebaseio.com/'); vm.syncObj = $firebaseObject(ref); //this will ensure your binding will work 这是我所拥有的,但我必须用 $.save() 更新 syncObj 对吗?
  • 是的,它会更新 syncObj 绑定。如果对您有帮助,请点赞。
【解决方案2】:

我最近开始研究 AngularFire,因为我是新手,在进行研发时我通过了这个: $bindTo() 对 $scope 有很强的依赖性。 对于参考,你可以看到这个Firebase 3-way data binding with ControllerAs syntax

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多