如何将作用域中存储为字符串的脚本标记输出到AngularJS模板?
我需要在模板中使用嵌入式脚本,但在脚本运行之前动态插入图像的路径 我编写了一个指令,将图像路径构建为范围变量,然后使用如何将作用域中存储为字符串的脚本标记输出到AngularJS模板?,angularjs,templates,angularjs-directive,script-tag,angularjs-interpolate,Angularjs,Templates,Angularjs Directive,Script Tag,Angularjs Interpolate,我需要在模板中使用嵌入式脚本,但在脚本运行之前动态插入图像的路径 我编写了一个指令,将图像路径构建为范围变量,然后使用$interpolate将其插入标记中 现在存储在$scope.tag中的引用(作为字符串)是我希望注入的代码,并像脚本标记那样解释/运行 然而,脚本标记作为字符串添加到我的模板中,我似乎无法确定需要采取什么额外步骤才能将脚本标记字符串作为脚本标记实际输出 以下是模板(在指令中称为templateUrl): 似乎: ... link: function(scope, elemen
$interpolate
将其插入
标记中
现在存储在$scope.tag
中的引用(作为字符串)是我希望注入的代码,并像脚本标记那样解释/运行
然而,脚本标记作为字符串添加到我的模板中,我似乎无法确定需要采取什么额外步骤才能将脚本标记字符串作为脚本标记实际输出
以下是模板(在指令中称为templateUrl
):
似乎:
...
link: function(scope, element, attrs) {
element.append( scope.tag );
}
...
它使用jQuery将脚本标记附加到指令的元素
'use strict';
angular.module('App')
.directive('sslLogo', function($location, $interpolate) {
return {
replace: false,
restrict: 'AE',
templateUrl: '/views/partials/ssl-logo.html',
controller: function($scope, $element, $attrs) {
// Outputs the full base url (http:// ... .com/ )
var base_url = '';
base_url += $location.protocol() + '://';
base_url += $location.host();
if ( $location.port() ) {
base_url += ':' + $location.port();
}
// Add the path to the image asset.
$scope.logo_url = base_url + "/images/static/comodo-wildcardssl.png";
// Interpolate this path into the script tag and then store the script tag to be output to the template.
$scope.tag = $interpolate('<script type="text/javascript">TrustLogo("{{ logo_url }}", "SCCC", "none");</script>', false, true)({ logo_url: $scope.logo_url });
},
link: function(scope, element, attrs) {}
};
});
<script type="text/javascript">TrustLogo("http://localhost:9000/images/static/comodo-wildcardssl.png", "SCCC", "none");</script>
...
link: function(scope, element, attrs) {
element.append( scope.tag );
}
...