在 angular material 对话框中操作 firebase 数组

manipulating a firebase array in a angular material dialog

我有一个 json 对象 posts 的 firebase 数组,它有一些键值对和一个数组,我正在使用 ng-重复以显示这些值和一个 material 对话框以添加注释。在对话框中,我从 ng-repeat 传递了 post 并将控制器附加到对话框,但是当我将注释推送到数组中时,它显示 属性 push is not defined for undefined 即使我的数组是用空白数组初始化的 这是我的 app.js

var app = angular.module('myapp', ['firebase', 'ngMaterial']);
    app.factory("posts", ["$firebaseArray",
                    function ($firebaseArray) {
            // create a reference to the database location where we will store our data
            //var randomRoomId = Math.round(Math.random() * 100000000);
            var ref = new Firebase('https://xxxxxxxxx.firebaseio.com/posts');

            // this uses AngularFire to create the synchronized array
            return $firebaseArray(ref);
            }
                                 ]);
    app.controller('mainCtrl', ['$scope', '$firebaseArray', 'posts', '$timeout', '$mdToast', '$mdDialog', function ($scope, $firebaseArray, posts, $timeout, $mdToast, $mdDialog) {
        $scope.posts = posts;
        $scope.post = {};
        $scope.loading = true;
        $scope.showForm = false;
        $timeout(function () {

            $scope.loading = false;
        }, 5000);

        $scope.createPost = function (post) {
            $scope.posts.$add({
                name: post.name,
                desc: post.desc,
                url: post.url,
                like: 0,
                dislike: 0,
                comments: []
            });
            $scope.myForm.$setPristine();
            $scope.myForm.$setUntouched();
            $scope.post = {};
            $scope.showForm = false;
            $mdToast.show(
                $mdToast.simple()
                .textContent('Post Added !!')
                .position("top right")
                .hideDelay(3000)
            );
        };
        $scope.showAddForm = function () {
            $scope.showForm = true;
        };
        $scope.clearForm = function () {
            $scope.showForm = false;
            $scope.myForm.$setPristine();
            $scope.myForm.$setUntouched();
            $scope.post = {};
        };

        $scope.showDialog = function (post) {


            $mdDialog.show({

                templateUrl: 'dialogtemp.html',
                locals: {
                    post: post,
                    posts: $scope.posts

                },
                controller: DialogController
            });
        }

        function DialogController($scope, $mdDialog, post, posts) {
            $scope.post = post;
            $scope.posts = posts;
            $scope.closeDialog = function () {
                $mdDialog.hide();
            };
            $scope.addComment = function () {

                $scope.post.comments.push({
                    "by": "jain",
                    "com": "heyy"
                });
                $scope.posts.$save($scope.post);
            };
        };

                }]);

可能是您的 $scope.post 尚未定义 comments 密钥。尝试先用空数组初始化它,然后 push()(当然要进行必要的检查)。一个简单的例子是。

var file = {};file.comments = [];file.comments.push({text:"hello"});//no error

但这会给你错误

var file = {};file.comments.push({text:"hello"});//error: Cannot read property 'push' of undefined(…)