angular的promise - on submit获得了"错误:args是null $parseFunct

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

本文关键字:args 错误 null parseFunct quot promise on submit 获得了 angular      更新时间:2023-09-26

我目前在MEAN.js: https://thinkster.io/mean-stack-tutorial/.

我被困在"布线一切"的结尾,我是一个全新的角度,所以我没有假装我理解我所做的一切。情况如下:

我们正在使用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);
        };
    }]);

最后,从远程web服务

检索帖子的工厂
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;
}]);

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

我怀疑问题来自于未关联的承诺和范围。我搜索了承诺,但我认为ui-router做得不同。

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

有人知道吗?提前谢谢大家

表单名称addComment(用于addComment.$valid)和添加到作用域的函数addComment相互冲突,请重命名其中一个。

参见Angular文档中的form指令:

如果指定了name属性,则发布表单控制器

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