【发布时间】:2016-05-30 15:48:48
【问题描述】:
TL;DR;
我想将替换标签 innerHtml 的指令写入其他标签属性,并使其编译。
表达式在ng-repeat 中(显然这很重要)。<ii>{{exp}}</ii> -> <i title="exp compiled!"></i>
说来话长
我有下一个指令:
<span ng-repeat="name in names">
<ii ng-class="{red: isRed}">Here comes HTML<br/> with {{name}} bla bla </ii>
</span>
附加到下一个控制器的指令:
EZcountApp.controller('myCtr',function($scope){
$scope.isRed = true;
$scope.names= ['Alon','Moshe','Zvi'];
});
我希望它被编译到下一个输出中:
<i class="fa fa-question-circle" ng-class="{red: isRed}" title="
Here comes HTML<br/> with {{name}} bla bla
"></i>
最后的输出应该是:
<span><i class="fa fa-question-circle red" title="Here comes HTML<br/> with Alon bla bla"></i></span>
<span><i class="fa fa-question-circle red" title="Here comes HTML<br/> with Moshe bla bla"></i></span>
<span><i class="fa fa-question-circle red" title="Here comes HTML<br/> with Zvi bla bla"></i></span>
我正在使用下一个指令:
myApp.directive('ii',function(){
return {
restrict: 'E',
transclude: 'element',
replace: true,
scope: {},
controller: function ($scope, $element, $attrs, $transclude) {
// if there is no class of fa-XXXX
// some login removed from here...
$element.addClass('fa-question-circle');
},
template: "<i class='fa help-icon' data-toggle='tooltip' data-placement='top' ng-transclude></i>"
};
});
问题是它不起作用,标题中的文本没有编译。
任何帮助将不胜感激。
这里附上一个错误的演示代码,你可以运行
myApp = angular.module('myApp', []);
myApp.directive('ii', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {},
controller: function($scope, $element, $attrs, $transclude) {
// if there is no class of fa-XXXX
// some logic removed from here...
$element.addClass('fa-question-circle');
//get data from the element and insert into title
$scope.title = $element.html();
//empty the element
$element.empty();
},
template: "<i class='fa help-icon' data-toggle='tooltip' data-placement='top' title='{{title}}' ng-transclude></i>"
};
});
myApp.controller('myCtr', function($scope) {
$scope.isRed = true;
$scope.names = ['Alon', 'Moshe', 'Zvi'];
});
.red {
color: red
}
<div ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<div ng-controller="myCtr">
actual result:
<span ng-repeat="name in names">
<ii ng-class="{red: isRed}" title="{{title}}"></ii><br/>
</span>
results in console (<b>title attr is empty</b>):
<pre>
<i class="fa help-icon ng-isolate-scope fa-question-circle red" data-toggle="tooltip" data-placement="top" title="" ng-transclude="" ng-class="{red: isRed}"></i>
<i class="fa help-icon ng-isolate-scope fa-question-circle red" data-toggle="tooltip" data-placement="top" title="" ng-transclude="" ng-class="{red: isRed}"></i>
<i class="fa help-icon ng-isolate-scope fa-question-circle red" data-toggle="tooltip" data-placement="top" title="" ng-transclude="" ng-class="{red: isRed}"></i>
</pre>
</div>
</div>
desired results
<pre>
<i title="Here comes HTML with Alon bla bla" class="fa help-icon ng-isolate-scope fa-question-circle red" ></i>
<i title="Here comes HTML with Moshe bla bla"class="fa help-icon ng-isolate-scope fa-question-circle red" ></i>
<i title="Here comes HTML with Zvi bla bla" class="fa help-icon ng-isolate-scope fa-question-circle red" ></i>
</pre>
【问题讨论】:
标签: angularjs angularjs-directive