角度承诺 - 提交获取"错误:args为null $ parseFunctionCall"

时间:2015-05-19 08:42:33

标签: javascript angularjs angular-ui-router angular-promise

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

我被困在了#34; Wiring Everything Up"的结尾,我是新的角色所以我不是假装我理解我所做的一切。情况如下:

我们正在使用插件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>

错误&#34;错误:args为null $ parseFunctionCall&#34;只有在我提交表格时才会发生

然后,这是此页面的配置步骤:

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但没有成功。

有人对此有所了解吗?提前谢谢

1 个答案:

答案 0 :(得分:0)

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

有关表单指令,请参阅Angular docs

  

如果指定了name属性,则发布表单控制器   以此名称加入当前范围。

当您手动添加名为addComment的函数时,在评估ng-submit时它使用了错误的函数。