这是为什么控制器不更新使用使用AngularJs $ Post方法后有何看法? [英] why is this controller not updating the view after using $Post method using AngularJs?

查看:96
本文介绍了这是为什么控制器不更新使用使用AngularJs $ Post方法后有何看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的角度应用程序我有一个控制器如下:

In my angular app I have a controller as follows:

mainCtrl.controller('mainController', function($scope, $http, Comment) {
        // GET ALL COMMENTS ====================================================
        Comment.getQuestion()
            .success(function(data) {
                $scope.qa = data;
            });

            $scope.qusdata = {};

            //Post Question 
            $scope.postquestion = function() {

            $scope.loading = true;

            Comment.PostQuestion($scope.qusdata)
                .success(function(data) {
                    console.log(data);
                    $scope.response = data;
                    $scope.loading = true;
                    // if successful, we'll need to refresh the comment list
                    Comment.getQuestion()
                        .success(function(getData) {
                            console.log(getData);
                            $scope.qa = getData;
                            $scope.loading = false;
                        });

                })
                .error(function(data) {
                    console.log(data);
                });
            };

        });

在视图里我有名单如下:

Within the view I have list as follows:

<div class="container"  ng-controller="mainController" >
        <div class="row"  ng-hide="loading" ng-repeat="q in qa.data" >
            <div class="col-md-12 col-xs-12 col.sm.12" >
                <div class="artist-data pull-left" style = "background-color:#BEB7B7;">
                    <!-- Div for user image -->
                    <div class="artst-pic pull-left">
                        <img width = 100px; height:60px; ng-src="{{image(q.userID)}}" alt="" class="img-responsive"/>
                    </div>
                    <!-- Div for voting for question-->
                    <div style = "margin-top:85px; margin-left: 10px;">
                        <div class=" pull-left" >
                            <a class="btn btn-xs btn-hover btn-primary" ng-click = "upVote(q.id)">
                                <span class="glyphicon glyphicon-thumbs-up"></span>
                            </a>
                            <input  class = "text" ng-model="q.votes"></input>
                            <a class="btn btn-xs btn-hover btn-danger" ng-click = "downVote(q.id)">
                                <span class="glyphicon glyphicon-thumbs-down"></span>
                            </a>
                        </div>
                    </div>
                    <!-- Div for imformation about the user -->
                    <div class="artst-prfle pull-left">
                        <div class = "col-md-12" >
                            <div class="art-title">
                                <p ng-bind-html = "q.question | emoji">{{ q.question}}</p>
                                <span class="artst-sub" >By 
                                    <span class="byname" >{{ q.users.first_name }} {{ q.users.last_name }}</span> 
                                    <span class="daysago">around {{ q.created_at }}</span>
                                </span>
                                <span>
                                    <div>
                                        <a class="glyphicon glyphicon-comment"></a> 
                                        <a ng-click="select(q.id)" >Answer</a>
                                    </div>
                                </span>
                            </div>
                        </div>
                    </div>

现在,当我在模板中点击添加问题按钮,然后,它调用postquestion方法和成功的POST调用的REST API进行。但鉴于没有更新,直到它被刷新。我在做什么错了?

Now when I click on the Add Question button in the template then, It calls the postquestion method and a successful POST call is made to the REST api. But the view does not get updated until it is refreshed. What am I doing wrong?

推荐答案

尝试使用此方法后:

 $scope.postquestion = function() {

            $scope.loading = true;

            Comment.PostQuestion($scope.qusdata)
                .success(function(data) {
                    console.log(data);
                    $scope.response = data;
                    $scope.loading = true;
                    // if successful, we'll need to refresh the comment list
                    Comment.getQuestion()
                        .success(function(getData) {
                            $scope.qa=null;
                            console.log(getData);
                            $scope.qa = getData;
                            $scope.loading = false;
                            $scope.$apply();
                        });

                })
                .error(function(data) {
                    console.log(data);
                });
            };

这篇关于这是为什么控制器不更新使用使用AngularJs $ Post方法后有何看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆