gpt4 book ai didi

javascript - 如何在更改变量后重新渲染指令?

转载 作者:数据小太阳 更新时间:2023-10-29 05:14:54 25 4
gpt4 key购买 nike

我正在使用星级评定指令。但是模板是在从 HTTP 加载数据之前加载的。所以我想在 HTTP 请求成功后重新加载指令模板。

HTML

<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js" type="text/javascript"></script>
</head><body>


<div ng-app="myapp" ng-controller="movieCtrl">

<div star-rating rating="starRating" read-only="false" max-rating="10" click="click(param)" mouse-hover="mouseHover(param)"
mouse-leave="mouseLeave(param)"></div>
</div></body></html>

JS

var app = angular.module('myapp', []);
app.controller("movieCtrl", function($scope, $http) {

$scope.starRating = 0;
$scope.hoverRating = 0;


$scope.mouseHover = function(param) {
$scope.hoverRating1 = param;
};

$scope.mouseLeave = function(param) {

$scope.hoverRating1 = param + '*';
};
//problem here
//actual data coming via http
//when value is changed i want to re-render below directive template
setTimeout(function() {
$scope.starRating = 5
}, 1000);
});
app.directive('starRating', function() {
return {
scope: {
rating: '=',
maxRating: '@',
readOnly: '@',
click: "&",
mouseHover: "&",
mouseLeave: "&"
},
restrict: 'EA',
template: "<div style='display: inline-block; margin: 0px; padding: 0px; cursor:pointer;' ng-repeat='idx in maxRatings track by $index'> \
<img ng-src='{{((hoverValue + _rating) <= $index) && \"http://www.codeproject.com/script/ratings/images/star-empty-lg.png\" || \"http://www.codeproject.com/script/ratings/images/star-fill-lg.png\"}}' \
ng-Click='isolatedClick($index + 1)' \
ng-mouseenter='isolatedMouseHover($index + 1)' \
ng-mouseleave='isolatedMouseLeave($index + 1)'></img> \
</div>",
compile: function(element, attrs) {
if (!attrs.maxRating || (Number(attrs.maxRating) <= 0)) {
attrs.maxRating = '5';
};
},
controller: function($scope, $element, $attrs) {
$scope.maxRatings = [];

for (var i = 1; i <= $scope.maxRating; i++) {
$scope.maxRatings.push({});
};

$scope._rating = $scope.rating;

$scope.isolatedClick = function(param) {
if ($scope.readOnly == 'true') return;

$scope.rating = $scope._rating = param;
$scope.hoverValue = 0;
$scope.click({
param: param
});
};

$scope.isolatedMouseHover = function(param) {
if ($scope.readOnly == 'true') return;

$scope._rating = 0;
$scope.hoverValue = param;
$scope.mouseHover({
param: param
});
};

$scope.isolatedMouseLeave = function(param) {
if ($scope.readOnly == 'true') return;

$scope._rating = $scope.rating;
$scope.hoverValue = 0;
$scope.mouseLeave({
param: param
});
};
}
};
});

参见 Codepen了解更多信息。

最佳答案

这是一个使用星星的简单评级指令,请注意逻辑在 link 函数中,而不是在 Controller 中。

function starRating() {
return {
restrict: 'EA',
template:
'<ul class="star-rating" ng-class="{readonly: readonly}">' +
// see ng-repeat here? this will update when scope.stars is updated
' <li ng-repeat="star in stars" class="star" ng-class="{filled: star.filled}" ng-click="toggle($index)">' +
' <i class="fa fa-star"></i>' + // or &#9733
' </li>' +
'</ul>',
scope: {
ratingValue: '=ngModel',
max: '=?', // optional (default is 5)
onRatingSelect: '&?', // callback
readonly: '=?' // set whether this should be changeable or not
},
link: function(scope, element, attributes) {
if (scope.max == undefined) {
scope.max = 5;
}
function updateStars() { // update to rating value
scope.stars = [];
for (var i = 0; i < scope.max; i++) {
scope.stars.push({
filled: i < scope.ratingValue
});
}
};
scope.toggle = function(index) {
if (scope.readonly == undefined || scope.readonly === false){
scope.ratingValue = index + 1;
scope.onRatingSelect({
rating: index + 1
});
}
};
scope.$watch('ratingValue', function(oldValue, newValue) {
if (newValue) {
updateStars();
}
});
}
};
}

关于javascript - 如何在更改变量后重新渲染指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34439722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com