Ala*_*-GI 6 javascript data-binding jquery angularjs angularjs-scope
我有一个名为"HomeCtrl"的控制器,它计算{{total}} 绑定变量中用户的总数,如下所示:
.controller('HomeCtrl', function($scope, $http){
$scope.total = 0;
});
Run Code Online (Sandbox Code Playgroud)
在我看来,我试图通过{{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>
Run Code Online (Sandbox Code Playgroud)
这是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();
});
}
};
})
Run Code Online (Sandbox Code Playgroud)
我可以在我的视图中显示{{total}}的正确值,但是当我传入{{total}}属性时data-to= "{{total}}",它不起作用.它不会将其识别为数字.
小智 4
这对我有用:当控制器加载时,使用异步 Web 服务调用检索号码。这意味着调用 sm.completelyEnteredViewport 函数时该值不存在。但幸运的是,您可以在 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>Run Code Online (Sandbox Code Playgroud)
然后像这样修改指令:从元素中检索延迟值并将其余代码放入延迟函数中,如下所示:
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();
});
}
};
}).Run Code Online (Sandbox Code Playgroud)