gpt4 book ai didi

javascript - Angular : use a variable into

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

我有一个名为“HomeCtrl”的 Controller ,它计算进入 {{total}} 的用户总数。绑定(bind)变量,像这样:

.controller('HomeCtrl', function($scope, $http){
$scope.total = 0;
});

在我看来,我试图通过传递 {{total}} 在动画小部件中显示我的总数。作为 <div> 上的属性值标签,像这样:

<div ng-controller="HomeCtrl" ng-init="init('users')">
<div class="xe-widget xe-counter xe-counter-green" xe-counter
data-count=".num" data-from="1"
data-to= "{{total}}"
data-suffix="users" data-duration="3" data-easing="true">
<div class="xe-icon">
<i class="linecons-user"></i>
</div>
<div class="xe-label">
<strong class="num">1k</strong>
<span>Users Total </span>
</div>
</div>
<center> <b> Total utilisateurs : {{total}} </b> </center>

这是小部件指令:

.directive('xeCounter', function(){     
return {
restrict: 'EAC',
link: function(scope, el, attrs)
{
var $el = angular.element(el),
sm = scrollMonitor.create(el);
sm.fullyEnterViewport(function()
{
var opts = {
useEasing: attrDefault($el, 'easing', true),
useGrouping: attrDefault($el, 'grouping', true),
separator: attrDefault($el, 'separator', ','),
decimal: attrDefault($el, 'decimal', '.'),
prefix: attrDefault($el, 'prefix', ''),
suffix: attrDefault($el, 'suffix', ''),
},
$count = attrDefault($el, 'count', 'this') == 'this' ? $el : $el.find($el.data('count')),
from = attrDefault($el, 'from', 0),
to = attrDefault($el, 'to', ''),
duration = attrDefault($el, 'duration', 2.5),
delay = attrDefault($el, 'delay', 0),
decimals = new String(to).match(/\.([0-9]+)/) ? new String(to).match(/\.([0-9]+)$/)[1].length : 0,
counter = new countUp($count.get(0), from, to, decimals, duration, opts);
setTimeout(function(){ counter.start(); }, delay * 1000);

sm.destroy();
});
}
};
})

我可以在我的 View 中显示 {{total}} 的正确值,但是当我通过 {{total}} 时进入属性,如data-to= "{{total}}" , 这是行不通的。它无法将其识别为数字。

最佳答案

这对我有用:当 Controller 加载时,使用异步 web 服务调用检索 Number。这意味着调用 sm.fullyEnteredViewport 函数时该值不存在。但幸运的是,您可以在 xe-counter 元素上设置数据延迟属性。

<div class="xe-widget xe-counter" xe-counter data-delay="1" data-count=".num" data-from="0" data-to="{{ total }}" data-suffix="" data-duration="5">
<div class="xe-icon"><i class="linecons-cup" /></div>
<div class="xe-label"><strong class="num">0</strong><span>TOTAL</span></div>
</div>

然后像这样修改你的指令:从元素中检索延迟值并将其他代码的其余部分放入延迟函数中,如下所示:

directive('xeCounter', function () {
return {
restrict: 'EAC',
link: function (scope, el, attrs) {

var $el = angular.element(el),
sm = scrollMonitor.create(el);

//get the delay attribute from element
var delay = attrs.delay ? attrs.delay : 0;

sm.fullyEnterViewport(function () {

setTimeout(function () {
// get all values from element delayed and start the counter
var opts = {
useEasing: attrDefault($el, 'easing', true),
useGrouping: attrDefault($el, 'grouping', true),
separator: attrDefault($el, 'separator', ','),
decimal: attrDefault($el, 'decimal', '.'),
prefix: attrDefault($el, 'prefix', ''),
suffix: attrDefault($el, 'suffix', '')
},
$count = attrDefault($el, 'count', 'this') == 'this' ? $el : $el.find($el.data('count')),
from = attrs.from ? attrs.from : 0,
to = attrs.to ? attrs.to : 100,
duration = attrs.duration ? attrs.duration : 2.5,
decimals = String(to).match(/\.([0-9]+)/) ? String(to).match(/\.([0-9]+)$/)[1].length : 0,
counter = new countUp($count.get(0), from, to, decimals, duration, opts);
counter.start();
}, delay * 1000);

sm.destroy();
});
}
};
}).
您的网络服务应在延迟时间内返回。这对我有用。

关于javascript - Angular : use a variable into <div>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27521271/

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