简体   繁体   English

ng-repeat中的AngularJS指令

[英]AngularJS Directives in ng-repeat

I'm trying to use Angular's ng-repeat to display jQuery sparklines. 我正在尝试使用Angular的ng-repeat显示jQuery迷你图。 I've wrapped the sparkline in what the angular directive to tell it to call. 我将迷你图包装在告诉它调用的angular指令中。 It seems that despite having a postlink in there it still is call the sparklines too early. 似乎尽管那里有一个后链接,但仍称呼迷你图为时过早。 I've attached my code below: 我在下面附加了我的代码:

var Timeline = angular.module('Timeline', []);
  Timeline.directive('sparkline', function () {
    return {
    restrict: 'E', 
    scope: { 
      val: '=',
    },
        link: function postLink(scope, iElement, attrs) {
      scope.$watch('scalars', function (newVal, oldVal) {
    $(iElement).sparkline('html', { 
           type:'bar', barColor:'#1e42c8', height:'40px', barWidth:10 , barSpacing:2
        });
      });
    }
  };
});

HTML 的HTML

<div class="row" ngChange:false ng-repeat="(description,scalars) in vitals | orderBy:orderProp">
  <div class="span4">
    <p><sparkline exp="data" class="sparkline">{{scalars.join(',')}}</sparkline></p>
  </div>
</div>

For anyone bouncing into this question: For a working sparklines directive, I suggest you check this fiddle out: http://jsfiddle.net/m48u/h8PdR/15/ ( not by me ). 对于任何遇到以下问题的人:对于有效的迷你图指令,建议您检查一下该提琴: http : //jsfiddle.net/m48u/h8PdR/15/不是我本人 )。 The directive itself looks like so - 该指令本身看起来像这样-

myapp.directive("bulletcharksparkline", function() {
return {
    restrict:"E",
    scope:{
        data:"@"
    },
    //template: "<div class='tpl'></div>",
    compile: function(tElement,tAttrs,transclude){
        tElement.replaceWith("<span>"+tAttrs.data+"</span>");
        return function(scope, element, attrs){
            attrs.$observe("data", function(newValue){
                element.html(newValue);
                element.sparkline('html',{type:'bullet'});
            });
        };
    }
};
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM