Javascript 在Angularjs中未使用ng bind html显示html标记
我有如下html模板: 我想使用“ng bind html”绑定此模板,如下所示:Javascript 在Angularjs中未使用ng bind html显示html标记,javascript,html,angularjs,Javascript,Html,Angularjs,我有如下html模板: 我想使用“ng bind html”绑定此模板,如下所示: angular.module('bindHtmlExample',['ngSanitize'])) .controller('ExampleController',['$scope','$compile',', 函数($scope$compile){ var模板html= '我是一个带有'+ ''; $scope.myHTML=$compile(templateHTML)($scope); } ]); 根
angular.module('bindHtmlExample',['ngSanitize']))
.controller('ExampleController',['$scope','$compile',',
函数($scope$compile){
var模板html=
'我是一个带有'+
'';
$scope.myHTML=$compile(templateHTML)($scope);
}
]);代码>
根据Vojta在以下方面的评论:
根据Vojta在中的注释,qLite在给定一个不以“开头的字符串时抛出:
根据Vojta在中的注释,qLite在给定一个不以“开头的字符串时抛出:
根据Vojta在中的注释,qLite在给定一个不以“开头的字符串时抛出:
当您使用$complie时,当给定一个不以“开头的字符串时,qLite将抛出,它将返回一个jqLite对象,而不是HTML字符串。但是,ngBindHTML变量的值应该是包含HTML的字符串。这就是为什么你看不到结果的原因
对于您的情况,最好使用指令而不是控制器来插入HTML。请参阅我的代码笔:
JS:
HTML:
使用$complie时,它将返回一个jqLite对象,而不是HTML字符串。但是,ngBindHTML变量的值应该是包含HTML的字符串。这就是为什么你看不到结果的原因
对于您的情况,最好使用指令而不是控制器来插入HTML。请参阅我的代码笔:
JS:
HTML:
使用$complie时,它将返回一个jqLite对象,而不是HTML字符串。但是,ngBindHTML变量的值应该是包含HTML的字符串。这就是为什么你看不到结果的原因
对于您的情况,最好使用指令而不是控制器来插入HTML。请参阅我的代码笔:
JS:
HTML:
使用$complie时,它将返回一个jqLite对象,而不是HTML字符串。但是,ngBindHTML变量的值应该是包含HTML的字符串。这就是为什么你看不到结果的原因
对于您的情况,最好使用指令而不是控制器来插入HTML。请参阅我的代码笔:
JS:
HTML:
我认为一个可能的解决方案是编写自己的指令,如
angular.module('bindHtmlExample',['ngSanitize']))
.controller('ExampleController',['$scope','$compile',函数($scope,$compile){
var模板html=
'我是一个带有'+
“我”;
$scope.myHTML=模板HTML;
$scope.refresh=函数(){
console.log('refresh')
};
}]);
angular.module('bindHtmlExample')。指令('myHtml',['$compile',函数($compile){
返回{
限制:“A”,
链接:函数链接($scope$element,attrs){
属性$observe('myHtml',函数(值){
var$el=$compile(value)($scope);
$element.empty().append($el)
})
}
}
}])
我认为一个可能的解决方案是编写自己的指令,如
angular.module('bindHtmlExample',['ngSanitize']))
.controller('ExampleController',['$scope','$compile',函数($scope,$compile){
var模板html=
'我是一个带有'+
“我”;
$scope.myHTML=模板HTML;
$scope.refresh=函数(){
console.log('refresh')
};
}]);
angular.module('bindHtmlExample')。指令('myHtml',['$compile',函数($compile){
返回{
限制:“A”,
链接:函数链接($scope$element,attrs){
属性$observe('myHtml',函数(值){
var$el=$compile(value)($scope);
$element.empty().append($el)
})
}
}
}])
我认为一个可能的解决方案是编写自己的指令,如
angular.module('bindHtmlExample',['ngSanitize']))
.controller('ExampleController',['$scope','$compile',函数($scope,$compile){
var模板html=
'我是一个带有'+
“我”;
$scope.myHTML=模板HTML;
$scope.refresh=函数(){
console.log('refresh')
};
}]);
angular.module('bindHtmlExample')。指令('myHtml',['$compile',函数($compile){
返回{
限制:“A”,
链接:函数链接($scope$element,attrs){
属性$observe('myHtml',函数(值){
var$el=$compile(value)($scope);
$element.empty().append($el)
})
}
}
}])
我认为一个可能的解决方案是编写自己的指令,如
angular.module('bindHtmlExample',['ngSanitize']))
.controller('ExampleController',['$scope','$compile',函数($scope,$compile){
var模板html=
var templateHTML =
'<div>I am an <code>HTML</code>string with ' +
'<span class="pointer"><i class="icon-refresh pointer" ng-click="refresh()"></i></span></div>';
angular.module('bindHtmlExample', ['ngSanitize'])
.controller('ExampleController', ['$scope', '$interpolate',
function($scope, $interpolate) {
var templateHTML =
'I am an <code>HTML</code>string with ' +
'<span class="pointer"><i class="icon-refresh pointer" ng-click="refresh()">{{ bindText }}</i></span>';
$scope.bindText = 'Refresh()';
$scope.refresh = function() {
console.log('refresh() runned.');
};
$scope.interpolate = $interpolate(templateHTML)($scope);
}
])
.directive('exampleDirective', ['$compile', function($compile){
return function(scope, element){
var templateHTML =
'<p>I am an <code>HTML</code>string with ' +
'<span class="pointer"><i class="icon-refresh pointer" ng-click="refresh()">{{ bindText }}</i></span></p>';
element.append( $compile(templateHTML)(scope) );
};
}]);
<div ng-app="bindHtmlExample">
<div ng-controller="ExampleController" example-directive>
<p ng-bind-html-unsafe="interpolate"></p>
<p ng-bind-html="interpolate"></p>
</div>
</div>