从数据库渲染指令模板装载值 [英] Load value from db on rendering directive template
问题描述
我被困在一个疑问
我有HTML:
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>
我有一个指令:
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的数据,如果确实如此,那么在渲染评级模板负荷的评级。但是,这是行不通的。
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.
控制台显示:
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.
推荐答案
我终于下解决它:
*移动数据库事务控制器并在页面加载阵列得到的评价值(宣布HTML的车身控制器)
*用于这个数组进行比较,并在指令集的评价值。因此,没有异步调用的问题。
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
这篇关于从数据库渲染指令模板装载值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!