<html> <body ng-app="app"> <div ng-controller="ExampleController"> <pre>{{xml | xslt:xslt}}</pre> </div> <script src="angular.js"></script> <script src="ng-xslt.js"></script> <script> var myApp = angular.module('app', ['ngXslt']); myApp.controller('ExampleController', ['$scope',function ($scope) { $scope.xml ='<?xml version="1.0" encoding="UTF-8"?><root><name>Angular XSLT module!</name></root>'; $scope.xslt = '<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:output method="html"/><xsl:template match="/"><xsl:text>Hello, my name is </xsl:text><b><xsl:value-of select="root/name"/></b></xsl:template></xsl:stylesheet>'; } ]); </script> </body> </html>
<html> <body ng-app="app"> <div ng-controller="ExampleController"> <div ng-bind-html="xml | xslt:xslt | sanitize"></div> </div> <script src="angular.js"></script> <script src="ng-xslt.js"></script> <script> var myApp = angular.module('app', ['ngXslt']); myApp.controller('ExampleController', ['$scope',function ($scope) { $scope.xml ='<?xml version="1.0" encoding="UTF-8"?><root><name>Angular XSLT module!</name></root>'; $scope.xslt = '<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:output method="html"/><xsl:template match="/"><xsl:text>Hello, my name is </xsl:text><b><xsl:value-of select="root/name"/></b></xsl:template></xsl:stylesheet>'; } ]); myApp.filter("sanitize", ['$sce', function($sce) { return function(htmlCode){ return $sce.trustAsHtml(htmlCode); } }]); </script> </body> </html>
<html> <body ng-app="app"> <div ng-controller="ExampleController"> <div ng-bind-html="xml | xslt:xslt | sanitize" compile-template></div> </div> <script src="angular.js"></script> <script src="ng-xslt.js"></script> <script> var myApp = angular.module('app', ['ngXslt']); myApp.controller('ExampleController', ['$scope',function ($scope) { $scope.xml ='<?xml version="1.0" encoding="UTF-8"?><root><name>Angular XSLT module!</name></root>'; $scope.xslt = '<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" version="1.0" ><xsl:output method="html"/><xsl:template match="/"><xsl:text>Hello, my name is </xsl:text><b><xsl:value-of select="root/name"/></b><input type="button" ng-click="clickMyXslt()" value="clickMyXslt"/></xsl:template></xsl:stylesheet>'; $scope.clickMyXslt = function() { alert("Yes, I am from clickMyXslt function!"); }; } ]); myApp.filter("sanitize", ['$sce', function($sce) { return function(htmlCode){ return $sce.trustAsHtml(htmlCode); } }]); myApp.directive('compileTemplate', function($compile, $parse){ return { link: function(scope, element, attr){ var parsed = $parse(attr.ngBindHtml); function getStringValue() { return (parsed(scope) || '').toString(); } //Recompile if the template changes scope.$watch(getStringValue, function() { $compile(element, null, -9999)(scope); //The -9999 makes it skip directives so that we do not recompile ourselves }); } } }); </script> </body> </html>
Source: https://habr.com/ru/post/268825/
All Articles