Load value from db on rendering directive template

前提是你 提交于 2019-12-24 12:38:30

问题


I am stuck in a doubt
I have html :

<div ng-repeat="mydata in data" class="ng-scope ng-binding">

   <p class="ng-binding">{{mydata.postdata}}</p> 
   <div my-rating rating-value="rating" data-cat="post" data-id="mydata.id" ></div>

   <div ng-repeat="childData in mydata.personRelatedData">
          {{childData.personName}}  
          <div my-rating rating-value="rating" data-cat="person" data-id="childData .id" >
   </div>
</div>

I have a Directive :

myDirectives.directive('myRating', function () {
  return {
    restrict: 'A',
    template: '<div><ul>' +
      '<li ng-repeat="i in getNumber(myNumber)" ng-click="toggle($index)" id=$index>' +
      '<div ng-switch="switchPoint<$index">'+
      '<div ng-switch-when=true><img ng-src="img/{{Unrated}}"></div>'+
      '<div ng-switch-when=false><img ng-src="img/{{Rated}}"></div>'+
      ' </div>' +
      '</li></ul></div>',

    scope: {
      ratingValue: '=',         
        dataCat: '=',  
        dataId: '=',  
      readonly: '@',
      onRatingSelected: '&'     
    },
    link: function (scope, elem, attrs) {       
      scope.myNumber = 5;       
      scope.getNumber = function(num) {
            return new Array(num);   
      }     

      // if myId exists in array tab[], which is  an array consisting of rated datas' ids      
      if (tab.indexOf(scope.myId+"") != -1) {
      console.log("ID in DB !!!" + scope.dtId);          
  db.transaction(function (tx) {
        //get Rating value of ID dt.ID
    }              
      }

      scope.toggle= function(val) {
        scope.ratingValue = val + 1;          
        scope.onRatingSelected({rating: null});
        scope.switchPoint = val;                    
      } 
    }
  }
}

Now what I am trying here is that on load of a rating template, i check whether the data with that ID exists in db, and if it does, then render that rating on load of the rating template. But this is not working.

Console Shows :

ID in DB !!! : ABC
ID in DB !!! : DEF
ID in DB !!! : GHI
Setting Switch Point to 2
Setting Switch Point to 5
Setting Switch Point to 4

The result is that the rating templates that I have rated, on page load do get rated, but to the max value. While the unrated ones are fine.


回答1:


I finally solved it by following :
* Moved DB transaction to a controller and got rating values in an array on page-load (declared controller in body of html)
* Used this array to compare and set rating values in directive. Thus no async calls issue



来源:https://stackoverflow.com/questions/20376626/load-value-from-db-on-rendering-directive-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!