コントローラから2つの値を取得するディレクティブを作成しました。 ngクラスは、値以下のクラスを星に適用することになっています。私が期待したとおりにやっていない。ng-class条件式がディレクティブテンプレートで機能しない
指令:
<star-rating-reviews
class="item-star-review"
review-count="{{item.reviewCount}}"
review-rate="{{item.rating}}"
ng-show="item.rating && item.rating >= 3">
</star-rating-reviews>
指令テンプレート:
<div class="star" ng-repeat="star in [1,2,3,4,5] track by $index">
<svg viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet"
ng-class="{'accent-contrast-fill': star <= startCount}"
class="accent-contrast-fill">
<g class="style-scope iron-icon">
<path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"></path>
</g>
</svg>
</div>
<div class="reviews">
{{reviewCount}}
</div>
NG-クラスが適切な星にクラスを追加していません。
指令:
batman.directives.StarRatingReviews = function($timeout, typeUtils) {
function link(scope, element, attributes, directiveCtrl) {
$timeout(function() {
if (scope.reviewRate && scope.reviewCount) {
directiveCtrl.reviewCount = typeUtils.toNumberStrict(scope.reviewCount);
directiveCtrl.starCount = typeUtils.toNumberStrict(scope.reviewRate);
if (!isNaN(directiveCtrl.reviewCount) &&
!isNaN(directiveCtrl.starCount) &&
directiveCtrl.starCount >= NumConstants_.MIN_STAR_COUNT_REQUIRED &&
directiveCtrl.reviewCount > NumConstants_.ZERO &&
directiveCtrl.reviewCount < NumConstants_.EXCEEDING_VALUE) {
directiveCtrl.setStarsAndReviews_();
scope.reviewCount = directiveCtrl.reviewCount;
scope.starCount = directiveCtrl.starCount;
}
}
});
}
return {
restrict: 'AE',
controller: gpa.layouts.batman.controller.StarRatingReviewsCtrl,
link: link,
scope: {
reviewCount: '@',
reviewRate: '@',
index: '@',
check: '@'
},
templateUrl: 'template.html'
};
};
「startCount」はどこから来て、その値は何ですか? – Lex
startCountは、メインコントローラからの属性review-rate = "{{item.rating}}"から渡されています。 item.ratingまたはitem.reviewCountを渡すオプションが開発者に用意されているためです。値は常に1〜5の数値です。 値がディレクティブに渡されると、ディレクティブはディレクティブコントローラで何らかのチェックを行い、その値をscope.starCountに格納します。したがって値は常に1から5の間です。 私が明確でなかった場合は、review-rate = "{{item.rating}}"が指令自体にあります。 – Aaron
不足している命令についてお詫び申し上げます。私は会社の方針のためにどれくらいのコードを共有しているか注意しなければならない。 – Aaron