繁体   English   中英

angularJs —在(链接)指令html内在a-href链接中工作,而在按钮中不工作

[英]angularJs — inside the (Link) directive html working in a-href link and not working in button

在(Link)指令html内工作在a-href链接中而不在按钮中工作

嗨,我是为常用指令创建的,在这里我需要3种类型的命令(链接,按钮,图标),链接指令可以正常工作,但按钮不起作用

  var app = angular.module('testApp', ['ngSanitize']); app.directive('comonLink', ['$http', function($http) { return { restrict: 'AEC', require: '', scope: { setValues:'@', }, template:'<div ng-bind-html="setValues" compile-template> </div>', link: function(scope, iElement, iAttrs, ngModelController) { var comonLink=iAttrs.comonLink; var splitValues=comonLink.split(","); if(splitValues[0]=='link'){ scope.setValues='<a href="#/'+splitValues[1]+'">'+splitValues[2]+'</a>'; } else if(splitValues[0]=='button'){ scope.setValues='<button type="button" class="btn btn-default btn-block">'+splitValues[2]+'</button> '; } else if(splitValues[0]=='icon'){ } } }; }]); 
  <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>testApp</title> <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"> <script data-require="angular.js@~1.3.15" data-semver="1.3.15" src="https://code.angularjs.org/1.3.15/angular.js"></script> <script data-require="angular-ui-select@*" data-semver="0.13.2" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.13.2/select.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-rc.0/angular-sanitize.js"></script> </head> <body class="hold-transition skin-blue sidebar-mini" data-ng-app="testApp" > <div class="wrapper"> <span comon-Link="link,opRegistration,LinkName,isNew"> </span> <span comon-Link="button,opRegistration,ButtonName,isNew"> </span> </div> </body> </html> 

将此过滤器添加到您的应用

 app.filter('unsafe', function ($sce) {
    return function (val) {
       return $sce.trustAsHtml(val);
    };
 });

像这样使用

var app = angular.module('testApp', ['ngSanitize']);
 app.directive('comonLink', ['$http', function($http) {
     ...
    template:'<div  ng-bind-html="setValues| unsafe" compile-template>   </div> <span> {{ setValues1 }}</span>',
    ...
   };
 }]);  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM