angular promise - on submit gets "Error: args is null $parseFunctionCall"

angular promise - on submit gets "Error: args is null $parseFunctionCall"

我目前正在关注 MEAN.js 上的这篇教程:https://thinkster.io/mean-stack-tutorial/ .

我陷入了 "Wiring Everything Up" 的末尾,我完全是 angular 的新手,所以我并不是假装我理解我所做的一切。这是情况:

我们正在使用插件 ui-router。

首先是 html 模板:

<form name="addComment" ng-submit="addComment.$valid && addComment()"novalidate>
    <div class="form-group">
        <input class="form-control" type="text" placeholder="Comment" ng-model="body" required/>
    </div>
    <button type="submit" class="btn btn-primary">Comment</button>
</form>

错误"Error: args is null $parseFunctionCall"仅在我提交表单时发生

那么,下面是这个页面的配置步骤:

app.config(['$stateProvider', '$urlRouterProvider',
    function ($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('posts', {
                url        : '/posts/{id}',
                templateUrl: '/posts.html',
                controller : 'PostsCtrl',
                resolve    : {
                    post: ['$stateParams', 'posts', function ($stateParams, posts) {
                        return posts.get($stateParams.id);
                    }]
                }
            });

        $urlRouterProvider.otherwise('home');
    }]);

那里是控制器:

app.controller('PostsCtrl', ['$scope', 'posts', 'post',
    function ($scope, posts, post) {
        $scope.post = post;

        $scope.addComment = function () {

            posts.addComment(post._id, {
                body  : $scope.body,
                author: 'user'
            }).success(function (comment) {
                $scope.post.comments.push(comment);
            });

            $scope.body = '';
        };

        $scope.incrementUpVote = function (comment) {
            posts.upvoteComment(post, comment);
        };
    }]);

最后,从远程网络服务检索帖子的工厂

app.factory('posts', ['$http', function ($http) {
    var o = {
        posts: []
    };

    o.get = function (id) {
        return $http.get('/posts/' + id).then(function (res) {
            return res.data;
        });
    };

    o.addComment = function (id, comment) {
        return $http.post('/posts/' + id + '/comments', comment);
    };

    return o;
}]);

我只给出了我认为相关的部分。

我怀疑问题出在未链接的承诺和范围上。我搜索了 promises,但我认为 ui-router 的做法有所不同。

我在控制器中尝试了一些 $watch 但没有成功。

有人知道吗?提前谢谢你

表单名称 addComment(用于 addComment.$valid)和添加到范围的函数 addComment 相互冲突,重命名一个或另一个。

请参阅 Angular docs 表单指令:

If the name attribute is specified, the form controller is published onto the current scope under this name.

由于您还手动添加了一个名为 addComment 的函数,因此在评估 ng-submit 时使用了错误的函数。