在渲染指令模板上从db加载值

时间:2013-12-04 13:26:01

标签: angularjs angularjs-directive angularjs-scope angular-ui

我被困在怀疑中 我有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>

我有一个指令:

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;                    
      } 
    }
  }
}

现在我在这里尝试的是,在加载评级模板时,我检查具有该ID的数据是否存在于db中,如果存在,则在评级模板的加载时呈现该评级。但这不起作用。

控制台显示:

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

结果是,我在页面加载时评级的评级模板会被评级,但是达到最大值。虽然未评级的是好的。

1 个答案:

答案 0 :(得分:0)

我终于解决了以下问题:
*将DB事务移动到控制器并在页面加载的数组中获得评级值(在html体中声明控制器)
*使用此数组来比较和设置指令中的评级值。因此,没有异步调用问题