我有一个名为“HomeCtrl”的控制器,用于计算{{total}}
绑定变量中用户的总数,如下所示:
.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>
这是widget指令:
.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();
});
}
};
})
我可以在我的视图中显示{{total}}的正确值,但是当我将{{total}}
传递给属性data-to= "{{total}}"
时,它不起作用。它不会将其识别为数字。
答案 0 :(得分:4)
这对我有用: 控制器加载时,使用异步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>
&#13;
然后像这样修改你的指令: 从element中检索延迟值,并将其余的其他代码放入延迟函数中,如下所示:
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();
});
}
};
}).
&#13;
答案 1 :(得分:1)
你必须要区分,因为如果你只是在你的指令模板中使用{{total}}它是有效的,因为如果在本地控制器中找不到,则angular会自动在父控制器中查找total。
另一方面,如果要在隔离范围中使用total的值,可以绑定到data-to属性,这可以通过以下代码实现:
app.directive('xeCounter', function(){
return {
restrict: 'EA',
scope: { to: '@' },
link: function(scope)
{
console.log(scope.to);
}
};
});
顺便说一下。我将限制从EAC更改为EA,否则您的指令将被应用两次并中断。而且你必须将星号放在属性值周围。
请参阅完整的工作示例here。
答案 2 :(得分:1)
这适合我。
替换你的指令:
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 = attrs.from ? attrs.from : 0,
to = attrs.to ? attrs.to : 100,
duration = attrs.duration ? attrs.duration : 2.5,
delay = attrs.delay ? attrs.delay : 0,
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);
setTimeout(function () {
counter.start();
}, delay * 1000);
sm.destroy();
});
}
};
}).
HTML应该保持这样:
<div class="xe-widget xe-counter" xe-counter 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>
确定 $ scope.total 变量已声明且其值为数字。