简体   繁体   English

如何通过角度方法返回数据库结果

[英]how to return database result through angular method

I want to know if its possible to return database result through angular method call within an ngRepeat template. 我想知道是否有可能通过ngRepeat模板中的角度方法调用返回数据库结果。 To clarify here is my template: 要澄清的是我的模板:

<div class="article-right" ng-repeat="localNews in allLocalNews | limitTo:5">
  <div class="article-title">
    <p>
      On {{localNews.dte | date}} 
      <a>Number of comment: {{getNumberOfComment(localNews.id)}} </a>
    </p>
  </div>

and here is my controller, Within my controller I have a method which accepts each id value of results in ngRepeat to fetch the number of comments from the database 这是我的控制器,在我的控制器内,我有一个方法可以接受ngRepeat中结果的每个id值,以从数据库中获取评论数

$scope.getNumberOfComment = function(id){ 
  var cObj = {id:id};   //get a news number of comments   
  return $http.post("shared/search/getNumberOfComment.cln.php",cObj).success(function(response){
        if(response != "failed"){
            return response;
        }
    }).error(function (response, status) {
        console.log(response);
    });
}

This scripts makes my browser to freeze as it goes into loop. 该脚本使我的浏览器在进入循环时冻结。 Any help will be highly appreciated. 任何帮助将不胜感激。

It probably loops because each digest cycle it will remake the calls to get the info. 它可能会循环,因为每个摘要循环都会重新进行调用以获取信息。 You better off having a variable that gets populated per item and calling something like ng-init on each element which will only get called once to initialise you item variable. 您最好不要为每个项目填充一个变量,然后在每个元素上调用ng-init之类的东西,该元素仅被调用一次即可初始化您的项目变量。 Does that make sense? 那有意义吗?

eg 例如

    <div class="article-right" ng-repeat="localNews in allLocalNews | limitTo:5">
      <div class="article-title">
        <p>
          On {{localNews.dte | date}} 
          <a ng-init=getNumberOfComment(localNews.id, localNews)>Number of comment: {{localNews.myVar}} </a>
        </p>
      </div>

  $scope.getNumberOfComment = function(id, localNews){ 
  var cObj = {id:id};   //get a news number of comments   
  return $http.post("shared/search/getNumberOfComment.cln.php",cObj).success(function(response){
        if(response === "passed"){
            localNews.myVar = response;
        }
    }).error(function (response, status) {
        console.log(response);
    });
}

The solution is simple, use get call on controller init to get all comments data. 解决方案很简单,使用控制器初始化上的get调用获取所有注释数据。 Then using ng-repeat iterate on this collection and show only those data you want for a proper news id. 然后在此集合上使用ng-repeat进行迭代,并仅显示您想要适当的新闻ID的数据。 If you want only a number of comments, here is the example, it is not tested but you should get the idea. 如果您只想发表评论,请看下面的示例,该示例未经测试,但您应该明白。 It requires from you to prepare API endpoint to return all the comments. 它要求您准备API端点以返回所有注释。

function CommentsController() {
  var vm = this;
  vm.comments = [];
  getComments();

  function getComments() {
    $http.get("your/api/getComments.cln.php").success(function(r){
      vm.comments = r;
    }
  }

  function getNumberOfComment(newsId) {
    return vm.comments.filter(function(comment){
      return comment.news_id === newsId;
    }).length;
  }
}


<div class="article-right" ng-repeat="localNews in allLocalNews | limitTo:5">
  <div class="article-title">
    <p>
     On {{localNews.dte | date}} 
    <a>Number of comment: {{$ctrl.getNumberOfComment(localNews.id)}} </a>
  </p>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM