在这里,我们将给大家分享关于angularjs–如何传递HTML到角度指令?的知识,让您更了解angularjs页面跳转传递参数的本质,同时也会涉及到如何更有效地angularjsng-bind-ht
在这里,我们将给大家分享关于angularjs – 如何传递HTML到角度指令?的知识,让您更了解angularjs页面跳转传递参数的本质,同时也会涉及到如何更有效地angularjs ng-bind-html 指令 对html标签转译、angularjs – angular ng-bind-html和其中的指令、angularjs – Angular指令在HTML表格中无法正常工作、angularjs – Karma’Unexpected Request’当测试角度指令,即使使用ng-html2js的内容。
本文目录一览:- angularjs – 如何传递HTML到角度指令?(angularjs页面跳转传递参数)
- angularjs ng-bind-html 指令 对html标签转译
- angularjs – angular ng-bind-html和其中的指令
- angularjs – Angular指令在HTML表格中无法正常工作
- angularjs – Karma’Unexpected Request’当测试角度指令,即使使用ng-html2js
angularjs – 如何传递HTML到角度指令?(angularjs页面跳转传递参数)
<div my-dir> <div></div> </div>
然后,有我的指令:
app.directive('myDir',[ '$compile',function($compile) { return { restrict: 'E',link: function(scope,iElement,iAttrs){ // assigning things from iAttrs to scope goes here },scope: '@',replace: false,templateUrl: 'myDir.html' }; }]);
然后有myDir.html,其中我定义了一个新的元素:
<div></div>
即使我设置替换为false,我失去了内部的内容,我想要保持div – 我对角度文档的理解是,这将被追加在我的模板后。有没有一些方法来保存这个(可能通过我的链接功能?),使结果将是
<div> <div></div> </div>
谢谢!
ng-transclude
,在指令选项中添加transclude:true,并将ng-transclude添加到模板中:
<divng-transclude></div>`
这个plunkr有一个例子,说明如何使用ng-transclude和模板,保留原来的dom元素。
http://plnkr.co/edit/efyAiTmcKjnhRZMTlNGf
angularjs ng-bind-html 指令 对html标签转译
文章参考
http://www.tuicool.com/articles/2eIrIz
http://www.runoob.com/angularjs/ng-ng-bind-html.html
在工作中遇到问题:用户在后台添加一篇新闻,在数据库中存储的是HTML代码,从数据库查询出来之后结果把HTML代码显示出来。
解决办法:使用ng-bind-html 指令,能够对HTML代码的标签转译,在浏览器中显示
ng-bind-html 指令会自动过滤掉标签内的样式?
所谓sce即“Strict Contextual Escaping”的缩写。翻译成中文就是“严格的上下文模式”也可以理解为安全绑定吧。
$sce干的事情来看就是将语境中存在的跨站攻击的风险给干掉.
我们返回的内容中包含一系列的html标记,它可以通过使用$sce.trustAsHtml()。该方法将值转换为特权所接受并能安全地使用“ng-bind-html”。所以,我们必须在我们的控制器中引入$sce服务.
代码如下
【1】angular源码分析:angular中入境检察官$sce
【2】野兽的 Angular 学习 - -sce和sce和sceDelegate
【3】$sce官方手册
angularjs – angular ng-bind-html和其中的指令
我有一个元素,我想绑定到它的html。
< div ng-bind-html =“details”upper>< / div>
这样可行。现在,随着它我也有一个指令绑定到html:
$ scope.details =’成功! < a href =“#/ details / 12”upper> details< / a>
但指令上面的div和anchor不评估。如何使它工作?
你需要用这个模式调用一个’compile’指令:
HTML:
<div compile="details"></div>
JS:
.directive('compile',['$compile',function ($compile) { return function(scope,element,attrs) { scope.$watch( function(scope) { // watch the 'compile' expression for changes return scope.$eval(attrs.compile); },function(value) { // when the 'compile' expression changes // assign it into the current DOM element.html(value); // compile the new DOM and link it to the current // scope. // NOTE: we only compile .childNodes so that // we don't get into infinite loop compiling ourselves $compile(element.contents())(scope); } ); }; }])
你可以看到一个工作fiddle of it here
angularjs – Angular指令在HTML表格中无法正常工作
这是HTML标记:
<body ng-app="myApp" ng-controller="MainCtrl"> <table> <thead> <tr> <th>col1</th> <th>col2</th> <th>col3</th> <th>col4</th> </tr> </thead> <tbody> <my-directive></my-directive> </tbody> </table> </body>
指令:
var app = angular.module('myApp',[]); app.controller('MainCtrl',function($scope) { $scope.data = ['value1','value2','value3','value4']; }); app.directive('myDirective',function () { return { restrict: "E",replace: true,scope:false,link: function (scope,element) { var html = angular.element('<tr></tr>'); angular.forEach(scope.data,function(value,index) { html.append('<td>'+value+'</td>'); }); element.replaceWith(html); } }; });
请使用下面的Plunker链接查看结果:
http://plnkr.co/edit/zc00RIUHWNYW36lY5rgv?p=preview
解决方法
app.directive('myDirective',function () { return { restrict: "A",index) { html.append('<td>'+value+'</td>'); }); element.replaceWith(html); } }; }); <table> <thead> <tr> <th>col1</th> <th>col2</th> <th>col3</th> <th>col4</th> </tr> </thead> <tbody> <tr my-directive></tr> </tbody> </table>
angularjs – Karma’Unexpected Request’当测试角度指令,即使使用ng-html2js
错误:意外请求:GET test-directive.html
我使用Karma和Jasmine来测试Angular中的指令。我已经看过类似的问题,在StackOverflow,但发现,在其他示例中尝试过的一切是无济于事。
代码结构
测试应用程序
-src
– 电源
–lib
–js
– 模块
— testDir
—- test.js
—- test-directive.html
– – 测试
—– test.spec.js
-测试
–config
— karma.conf.js
–e2e
Karma配置
'use strict'; module.exports = function(config){ config.set({ basePath: '../../',frameworks: ['jasmine'],files: [ // Angular 'src/bower/angular/angular.js',// Mocks 'src/bower/angular-mocks/angular-mocks.js',// Libraries 'src/lib/**/*.js',// App 'src/js/*.js','src/modules/*/*.js',// Tests 'src/modules/**/test/*spec.js',// Templates 'src/modules/**/*.html' ],autoWatch: false,singleRun: true,reporters: ['progress'],browsers: ['PhantomJS'],preprocessors: { 'src/modules/**/*.html': 'ng-html2js' },ngHtml2JsPreprocessor: { moduleName: 'dir-templates' },plugins: [ 'karma-jasmine','karma-ng-html2js-preprocessor','karma-phantomjs-launcher','karma-chrome-launcher','karma-junit-reporter' ] }); };
test.js
'use strict'; angular.module('modules.test',[]). directive('testDirective',[function() { return { restrict: 'E',templateUrl: 'test-directive.html',link: function($scope,$elem,$attrs) { $scope.someFn = function() { angular.noop(); }; } }; }]);
test-direct.html
<span>Hello World</span>
test.spec.js
'use strict'; describe('test module',function() { beforeEach(module('modules.test')); /* -- DIRECTIVES------------------ */ describe('directives',function() { var $compile,$scope,elm; beforeEach(module('dir-templates'); beforeEach(inject(function($compile,$rootScope) { $scope = $rootScope.$new(); elm = angular.element('<test-directive></test-directive>'); $compile(elm)($scope); $scope.$digest(); })); it('should have one span tag',function(){ //Jasmine test here to check for one span tag. }); }); });
缩短了几个文件,以坚持到问题所在。在调用beforeEach(module(‘dir-templates’))时,应该将所有匹配的.html文件加载到$ templateCache中,并阻止GET请求抛出错误。
任何帮助将不胜感激,因为它真的一直驱动我坚果。如果您有任何其他问题,请发表评论。
简而言之,’src / modules / test / test-directive.html.js’!==’test-directive.html.js’。
为了实现这一点,修改karma.conf.js文件ngHtml2JsProcessor如下:
ngHtml2JsPreprocessor: { stripPrefix: 'src/',moduleName: 'dir-templates' },
和指令声明的templateUrl看起来像:
templateUrl: 'modules/test/test-directive.html'
关于angularjs – 如何传递HTML到角度指令?和angularjs页面跳转传递参数的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于angularjs ng-bind-html 指令 对html标签转译、angularjs – angular ng-bind-html和其中的指令、angularjs – Angular指令在HTML表格中无法正常工作、angularjs – Karma’Unexpected Request’当测试角度指令,即使使用ng-html2js等相关内容,可以在本站寻找。
本文标签: