javascript - HTML is not compiled inside angular directive -
angular.module("vtapp.directives"). directive('multirangeslider', function ($compile) { return { restrict: 'e', replace: true, link: function (scope, element, attrs) { var variances, values, options, template, compile; scope.variances = eval (attrs.variances); scope.values = scope.$eval(attrs.values); var htmltext = '<div id="'+attrs.id+'"><table class="crc"><tbody><tr>'; (var i=0; i<scope.values.length; i++) { htmltext += '<td id="val' + + '" style="width:' + scope.values[i] + '%;"> ' + scope.variances[i] + ': <strong>{{ranges[' + + ']}}%</strong></td>'; } htmltext += '</tr></tbody></table></div>'; template = angular.element(htmltext); $compile(template)(scope); element.replacewith(htmltext); } } });
and inside view have:
<multirangeslider values="ranges" variances="['master', 'template a', 'template b']"></multirangeslider>
the html not compiled. here jsfiddle
i update fiddle, it's work
http://jsfiddle.net/timactive/sxsxf/2/
var myapp = angular.module('myapp',[]); //myapp.directive('mydirective', function() {}); //myapp.factory('myservice', function() {}); function myctrl($scope) { $scope.name = 'superhero'; $scope.ranges = [33, 33, 34]; } myapp. directive('multirangeslider', function ($compile) { return { restrict: 'e', scope :{ranges:'='}, replace: true, link: function (scope, element, attrs) { var variances, values, options, template, compile; scope.variances = eval(attrs.variances); // scope.values = scope.$eval(attrs.values); var htmltext = '<div id="' + attrs.id + '"><table width="100%" cellspacing="0" cellpadding="0" border="1"><tbody><tr>'; (var = 0; < scope.ranges.length; i++) { console.log(scope.ranges[i]+" " + scope.variances[i]); htmltext += '<td id="val' + + '" style="width:' + scope.ranges[i] + '%;"> ' + scope.variances[i] + ': <strong>{{ranges[' + + ']}}%</strong></td>'; } htmltext += '</tr></tbody></table></div>'; console.log(htmltext); //htmltext += "{{ranges | json}}"; template = angular.element($compile(htmltext)(scope)); //$compile(template)(scope); element.replacewith(template); } } });
html :
<div ng-controller="myctrl"> {{name}} {{ranges | json}} <multirangeslider ranges="ranges" variances="['master', 'template a', 'template b']"></multirangeslider> <br /> range1 : <input type="text" ng-model="ranges[0]"/> range2 : <input type="text" ng-model="ranges[1]"/> range3 : <input type="text" ng-model="ranges[2]"/> </div>
Comments
Post a Comment