angualrjs 自定义css异步加载指令

html中调用:javascript

<!-- 异步加载css文件 -->
<load-css href="css/demo.css"></load-css>

 js代码:css

/**
 * 将对应路径的css插入html页面中
 */
app.directive('loadCss', function() {
    return {
        require: '?ngModel',
        restrict: 'E',  
        link: function ($scope, element, attrs, ngModel) {
            $("<link />")
            .prop("href", attrs.href)
            .prop("type", "text/css")
            .prop("rel", "stylesheet")
            .appendTo("head");
        }  
    }
});

 

 

欢迎加入,Java,前端的共同窗习【爱问共享编程部落】 479668591html

相关文章
相关标签/搜索