无法获取发布$ http服务angularjs

时间:2018-09-28 03:09:39

标签: angularjs

有人可以帮助我。 我的服务:(服务名称:reviewService)

this.addFeedback = function (param) {
    return httpService.post('my api', param);
}

我的控制器:

$scope.param= {
    ParentId: null, 
    Comment: ''
};

//Reply Button
$scope.Reply= function () {

};
//Submit Reply button on Popup
$scope.Submit = function () {
    reviewService.addFeedback($scope.param).then(function (response) {
        if (response.data.IsSuccess) {
            $scope.Array = response.data.Value;
        }
    });
};

我的想法: ** ParentId是用户评论的ID(项目) 单击“回答”按钮以回复用户评论时。我要获取项目单击的ID,并将其设置为对象的ParentId。然后显示弹出窗口,插入回复并提交。 请帮帮我!

2 个答案:

答案 0 :(得分:0)

您是否将$ http注入到控制器/工厂中? 例如:

app.controller('myCtrl', function($scope, $http) {
    $http.get("welcome.htm")
    .then(function(response) {
        $scope.myWelcome = response.data;
    });
});

答案 1 :(得分:0)

希望这会对您有所帮助:)

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, reviewService) {
    $scope.comments = [{ParentId: "1", Comment: "Click me 1"}, {ParentId: "2", Comment: "Click me 2"}];
    $scope.param= {
        ParentId: null, 
        Comment: ''
    };
    
    $scope.addFeedback = function (comment) {
        $scope.param.ParentId = comment.ParentId;
        $scope.param.Comment = comment.Comment;

        //Get the comment Id from UI and pass it into the service
        reviewService.addFeedback($scope.param).then(function (response) {
            if (response.data.IsSuccess) {
                $scope.Array = response.data.Value;
            }
        });
    }
});

app.factory('reviewService', function($http){
 var factory = {
     addFeedback: function (feedback) {
         var promise = new Promise(function(resolve, reject){
             //we will call $http service here
             //return httpService.post('my api', param);
             
             //mockup data
             console.log(feedback);
             setTimeout(function(){
                 resolve(feedback); 
             }, 1000);
             
         });
         
         return promise;
         
     }
 }
 
 return factory;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <div ng-repeat="comment in comments" ng-click="addFeedback(comment)">{{comment.Comment}}</div>
</div>