use*_*863 8 javascript angularjs angularjs-directive angular-services
我正在尝试通过角度服务编译指令但不幸的是它不起作用.想法是在弹出窗口中显示错误.
我修改了$ exceptionHandler服务:
crm.factory('$exceptionHandler', function(popup) {
return function(exception) {
popup.open({message: exception});
}
});
Run Code Online (Sandbox Code Playgroud)
在弹出的服务如下:
crm.factory('popup', function ($document) {
return {
open: function (data) {
var injector = angular.element(document).injector(),
$compile = injector.get('$compile'),
template = angular.element('<popup></popup>');
// var ctmp = $compile(template.contents());
$compile(template.contents());
$document.find('body').append(template);
}
};
});
Run Code Online (Sandbox Code Playgroud)
而且我不认为这是硬编码$ compile服务的好主意(但我没有任何想法如何在角度中实现这一点):
$compile = injector.get('$compile')
Run Code Online (Sandbox Code Playgroud)
弹出指令:
crm.directive('popup', function () {
return {
restrict: 'E',
replace: true,
templateUrl: '/public/js/templates/common/popup.html',
link: function() {
console.log('link()');
},
controller: function () {
console.log('ctrl()');
}
};
});
Run Code Online (Sandbox Code Playgroud)
可能还有其他一些方法可以做到这一点?谢谢.
您可以$compile直接注入到您的服务中,但您也没有完全$compile正确使用:
//commented alternative lines for allowing injection and minification since reflection on the minified code won't work
//crm.factory('popup', ['$document', '$compile', function ($document, $compile) {
crm.factory('popup', function ($document, $compile) {
return {
open: function (data) {
var template = angular.element('<popup></popup>'),
compiled = $compile(template);
$document.find('body').append(compiled);
}
};
});
//closing bracket for alternative definition that allows minification
//}]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5453 次 |
| 最近记录: |