繁体   English   中英

AngularFire-TypeError:无法读取未定义的属性“ $ add”

[英]AngularFire - TypeError: Cannot read property '$add' of undefined

我使用Ionic和AngularFire进行聊天,但出现问题:以下错误

TypeError:无法读取Scope。$ scope.sendChat中未定义的属性'$ add'(controllers.js:12)

妨碍我前进。 我不知道在哪里可以找到解决方案。

这是我在controller.JS中的代码:

angular.module('starter.controllers', ['firebase'])

.factory("Chats", function($firebaseArray) {
  var itemsRef = new Firebase("https://<MYFIREBASE>.firebaseio.com/chats");
  return $firebaseArray(chatsRef);
})

.controller('ChatsCtrl', ['$scope', '$rootScope', 
  function($scope, Chats, $rootScope) {

  $scope.sendChat = function(chat) {
      $scope.chats.$add({
        user: 'guest',
        message: chat.message

      });

      chat.message = "";

    };

}])

这是我的HTML代码:

<ion-view view-title="Chats">
  <ion-content>
    <div class="list">
      <a class="item item-avatar">
        <img>
        <h2>{{ chat.user }}</h2>
        <p>{{ data.text }}</p>
      </a>
    </div>
    <form>
      <div class="list">
        <div class="item item-input-inset">
          <label class="item-input-wrapper">
            <input type="text" placeholder="Message" ng-model="chat.message">
          </label>
          <button class="button button-assertive ion ion-ios-chatbubble" ng-click="sendChat(chat)"></button>
        </div>
      </div>
    </form>
  </ion-content>
</ion-view>

我可以找到一个慈善的灵魂来帮助我吗?

尝试这个,

angular.module('starter.controllers', ['firebase'])
    .factory("Chats", function($firebaseArray) {
        var itemsRef = new Firebase("https://<MYFIREBASE>.firebaseio.com/chats");
        return $firebaseArray(itemsRef);
    })
    .controller('ChatsCtrl', ['$scope', '$rootScope', 'Chats', '$firebaseArray',
        function($scope, $rootScope, Chats, $firebaseArray) {
            $scope.chats = Chats;
            $scope.sendChat = function(chat) {
                $scope.chats.$add({
                    user: 'guest',
                    message: chat.message

                });

                chat.message = "";

            };

        }
    ]);        

您不应该在聊天服务中返回$firebaseArray(itemsRef)而不是$firebaseArray(chatsRef)吗? 错字了吗?

在控制器中注入了Chats服务$firebaseArray

希望这可以帮助!

最明显的问题是您需要在Controller中引用$ firebaseArray或$ firebaseObject模块才能使用AngularFire方法。

.controller('ChatsCtrl', ['$scope', '$rootScope', '$firebaseArray',  function($scope, Chats, $rootScope, $firebaseArray) {

但是,如果这是您完成的Controller代码,那么还有其他问题,例如您需要使用Service数据填充$ scope.chats对象

$scope.chats = Chats;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM