Angularjs 如何在Angular指令中包装JS脚本

Angularjs 如何在Angular指令中包装JS脚本,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我正在学习AngularJS,我有一个我不久前编写的传统JS脚本,我想在我的新Angular应用程序中使用它 我可以直接将整个脚本转储到指令中,还是需要将关键字this更改为元素等 directive.directive("skillLevel", ['$timeout', function($timeout) { return{ link: function(scope, el, atts){ // CAN I PASTE MY SCRIPT HERE?? } }]);

我正在学习AngularJS,我有一个我不久前编写的传统JS脚本,我想在我的新Angular应用程序中使用它

我可以直接将整个脚本转储到指令中,还是需要将关键字
this
更改为
元素

directive.directive("skillLevel", ['$timeout', function($timeout) {
return{
    link: function(scope, el, atts){

       // CAN I PASTE MY SCRIPT HERE??
}
}]);
我有这个“相当长的一些”脚本,我想使用。我如何在指令中有效地使用它

(function ($) {
'use strict';

var RSS = function (target, url, options, callback) {
    this.target       = target;

    this.url          = url;
    this.html         = [];
    this.effectQueue  = [];

    this.options = $.extend({
        ssl: false,
        host: 'www.feedrapp.info',
        limit: null,
        key: null,
        layoutTemplate: '<ul>{entries}</ul>',
        entryTemplate: '<li><a href="{url}">[{author}@{date}] {title}</a><br/>{shortBodyPlain}</li>',
        tokens: {},
        outputMode: 'json',
        dateFormat: 'dddd MMM Do',
        dateLocale: 'en',
        effect: 'show',
        offsetStart: false,
        offsetEnd: false,
        error: function () {
            console.log('jQuery RSS: url doesn\'t link to RSS-Feed');
        },
        onData: function () {},
        success: function () {}
    }, options || {});

    // The current SSL certificate is only valid for *.herokuapp.com
    if (this.options.ssl && (this.options.host === 'www.feedrapp.info')) {
        this.options.host = 'feedrapp.herokuapp.com';
    }

    this.callback = callback || this.options.success;
};

RSS.htmlTags = [
    'doctype', 'html', 'head', 'title', 'base', 'link', 'meta', 'style', 'script', 'noscript',
    'body', 'article', 'nav', 'aside', 'section', 'header', 'footer', 'h1-h6', 'hgroup', 'address',
    'p', 'hr', 'pre', 'blockquote', 'ol', 'ul', 'li', 'dl', 'dt', 'dd', 'figure', 'figcaption',
    'div', 'table', 'caption', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td', 'col', 'colgroup',
    'form', 'fieldset', 'legend', 'label', 'input', 'button', 'select', 'datalist', 'optgroup',
    'option', 'textarea', 'keygen', 'output', 'progress', 'meter', 'details', 'summary', 'command',
    'menu', 'del', 'ins', 'img', 'iframe', 'embed', 'object', 'param', 'video', 'audio', 'source',
    'canvas', 'track', 'map', 'area', 'a', 'em', 'strong', 'i', 'b', 'u', 's', 'small', 'abbr', 'q',
    'cite', 'dfn', 'sub', 'sup', 'time', 'code', 'kbd', 'samp', 'var', 'mark', 'bdi', 'bdo', 'ruby',
    'rt', 'rp', 'span', 'br', 'wbr'
];

RSS.prototype.load = function (callback) {
    var apiProtocol = 'http' + (this.options.ssl ? 's' : '');
    var apiHost     = apiProtocol + '://' + this.options.host;
    var apiUrl      = apiHost + '?callback=?&q=' + encodeURIComponent(this.url);

    // set limit to offsetEnd if offset has been set
    if (this.options.offsetStart && this.options.offsetEnd) {
        this.options.limit = this.options.offsetEnd;
    }

    if (this.options.limit !== null) {
        apiUrl += '&num=' + this.options.limit;
    }

    if (this.options.key !== null) {
        apiUrl += '&key=' + this.options.key;
    }

    $.getJSON(apiUrl, callback);
};

RSS.prototype.render = function () {
    var self = this;

    this.load(function (data) {
        try {
            self.feed    = data.responseData.feed;
            self.entries = data.responseData.feed.entries;
        } catch (e) {
            self.entries = [];
            self.feed    = null;
            return self.options.error.call(self);
        }

        var html = self.generateHTMLForEntries();

        self.target.append(html.layout);

        if (html.entries.length !== 0) {
            if ($.isFunction(self.options.onData)) {
                self.options.onData.call(self);
            }

            self.appendEntriesAndApplyEffects($('entries', html.layout), html.entries);
        }

        if (self.effectQueue.length > 0) {
            self.executeEffectQueue(self.callback);
        } else if ($.isFunction(self.callback)) {
            self.callback.call(self);
        }
    });
};

RSS.prototype.appendEntriesAndApplyEffects = function (target, entries) {
    var self = this;

    $.each(entries, function (idx, entry) {
        var $html = self.wrapContent(entry);

        if (self.options.effect === 'show') {
            target.before($html);
        } else {
            $html.css({ display: 'none' });
            target.before($html);
            self.applyEffect($html, self.options.effect);
        }
    });

    target.remove();
};

RSS.prototype.generateHTMLForEntries = function () {
    var self   = this;
    var result = { entries: [], layout: null };

    $(this.entries).each(function () {
        var entry       = this;
        var offsetStart = self.options.offsetStart;
        var offsetEnd   = self.options.offsetEnd;
        var evaluatedString;

        // offset required
        if (offsetStart && offsetEnd) {
            if (index >= offsetStart && index <= offsetEnd) {
                if (self.isRelevant(entry, result.entries)) {
                    evaluatedString = self.evaluateStringForEntry(
                        self.options.entryTemplate, entry
                    );

                    result.entries.push(evaluatedString);
                }
            }
        } else {
            // no offset
            if (self.isRelevant(entry, result.entries)) {
                evaluatedString = self.evaluateStringForEntry(
                    self.options.entryTemplate, entry
                );

                result.entries.push(evaluatedString);
            }
        }
    });

    if (!!this.options.entryTemplate) {
        // we have an entryTemplate
        result.layout = this.wrapContent(
            this.options.layoutTemplate.replace('{entries}', '<entries></entries>')
        );
    } else {
        // no entryTemplate available
        result.layout = this.wrapContent('<div><entries></entries></div>');
    }

    return result;
};

RSS.prototype.wrapContent = function (content) {
    if ($.trim(content).indexOf('<') !== 0) {
        // the content has no html => create a surrounding div
        return $('<div>' + content + '</div>');
    } else {
        // the content has html => don't touch it
        return $(content);
    }
};

RSS.prototype.applyEffect = function ($element, effect, callback) {
    var self = this;

    switch (effect) {
        case 'slide':
            $element.slideDown('slow', callback);
            break;
        case 'slideFast':
            $element.slideDown(callback);
            break;
        case 'slideSynced':
            self.effectQueue.push({ element: $element, effect: 'slide' });
            break;
        case 'slideFastSynced':
            self.effectQueue.push({ element: $element, effect: 'slideFast' });
            break;
    }
};

RSS.prototype.executeEffectQueue = function (callback) {
    var self = this;

    this.effectQueue.reverse();

    var executeEffectQueueItem = function () {
        var item = self.effectQueue.pop();

        if (item) {
            self.applyEffect(item.element, item.effect, executeEffectQueueItem);
        } else if (callback) {
            callback();
        }
    };

    executeEffectQueueItem();
};

RSS.prototype.evaluateStringForEntry = function (string, entry) {
    var result = string;
    var self   = this;

    $(string.match(/(\{.*?\})/g)).each(function () {
        var token = this.toString();

        result = result.replace(token, self.getValueForToken(token, entry));
    });

    return result;
};

RSS.prototype.isRelevant = function (entry, entries) {
    var tokenMap = this.getTokenMap(entry);

    if (this.options.filter) {
        if (this.options.filterLimit && (this.options.filterLimit === entries.length)) {
            return false;
        } else {
            return this.options.filter(entry, tokenMap);
        }
    } else {
        return true;
    }
};

RSS.prototype.getFormattedDate = function (dateString) {
    // If a custom formatting function is provided, use that.
    if (this.options.dateFormatFunction) {
        return this.options.dateFormatFunction(dateString);
    } else if (typeof moment !== 'undefined') {
        // If moment.js is available and dateFormatFunction is not overriding it,
        // use it to format the date.
        var date = moment(new Date(dateString));

        if (date.locale) {
            date = date.locale(this.options.dateLocale);
        } else {
            date = date.lang(this.options.dateLocale);
        }

        return date.format(this.options.dateFormat);
    } else {
        // If all else fails, just use the date as-is.
        return dateString;
    }
};

RSS.prototype.getTokenMap = function (entry) {
    if (!this.feedTokens) {
        var feed = JSON.parse(JSON.stringify(this.feed));

        delete feed.entries;
        this.feedTokens = feed;
    }

    return $.extend({
        feed:      this.feedTokens,
        url:       entry.link,
        author:    entry.author,
        date:      this.getFormattedDate(entry.publishedDate),
        title:     entry.title,
        body:      entry.content,
        shortBody: entry.contentSnippet,

        bodyPlain: (function (entry) {
            var result = entry.content
                .replace(/<script[\\r\\\s\S]*<\/script>/mgi, '')
                .replace(/<\/?[^>]+>/gi, '');

            for (var i = 0; i < RSS.htmlTags.length; i++) {
                result = result.replace(new RegExp('<' + RSS.htmlTags[i], 'gi'), '');
            }

            return result;
        })(entry),

        shortBodyPlain: entry.contentSnippet.replace(/<\/?[^>]+>/gi, ''),
        index:          $.inArray(entry, this.entries),
        totalEntries:   this.entries.length,

        teaserImage:    (function (entry) {
            try {
                return entry.content.match(/(<img.*?>)/gi)[0];
            }
            catch (e) {
                return '';
            }
        })(entry),

        teaserImageUrl: (function (entry) {
            try {
                return entry.content.match(/(<img.*?>)/gi)[0].match(/src="(.*?)"/)[1];
            }
            catch (e) {
                return '';
            }
        })(entry)
    }, this.options.tokens);
};

RSS.prototype.getValueForToken = function (_token, entry) {
    var tokenMap = this.getTokenMap(entry);
    var token    = _token.replace(/[\{\}]/g, '');
    var result   = tokenMap[token];

    if (typeof result !== 'undefined') {
        return ((typeof result === 'function') ? result(entry, tokenMap) : result);
    } else {
        throw new Error('Unknown token: ' + _token + ', url:' + this.url);
    }
};

$.fn.rss = function (url, options, callback) {
    new RSS(this, url, options, callback).render();
    return this; // Implement chaining
};
})(jQuery);
(函数($){
"严格使用",;
var RSS=函数(目标、url、选项、回调){
this.target=目标;
this.url=url;
this.html=[];
this.effectQueue=[];
this.options=$.extend({
ssl:false,
主持人:“www.feedrapp.info”,
限制:空,
key:null,
layoutTemplate:“
    {entries}
”, 入口模板:“

  • {shortBodyPlain}
  • ”, 标记:{}, outputMode:'json', 日期格式:“dddd MMM Do”, dateLocale:'en', 效果:'显示', offsetStart:false, offsetEnd:false, 错误:函数(){ log('jqueryrss:url不链接到RSS提要'); }, onData:函数(){}, 成功:函数(){} },选项|{}); //当前SSL证书仅对*.herokuapp.com有效 if(this.options.ssl&&(this.options.host==='www.feedrapp.info')){ this.options.host='feedrapp.herokuapp.com'; } this.callback=callback | | this.options.success; }; RSS.htmlTags=[ “doctype”、“html”、“head”、“title”、“base”、“link”、“meta”、“style”、“script”、“noscript”, “正文”、“文章”、“导航”、“旁白”、“章节”、“页眉”、“页脚”、“h1-h6”、“hgroup”、“地址”, “p”、“hr”、“pre”、“blockquote”、“ol”、“ul”、“li”、“dl”、“dt”、“dd”、“figcaption”, “div”、“table”、“caption”、“thead”、“tbody”、“tfoot”、“tr”、“th”、“td”、“col”、“colgroup”, “表单”、“字段集”、“图例”、“标签”、“输入”、“按钮”、“选择”、“数据列表”、“optgroup”, “选项”、“文本区域”、“键根”、“输出”、“进度”、“仪表”、“详细信息”、“摘要”、“命令”, “menu”、“del”、“ins”、“img”、“iframe”、“嵌入”、“对象”、“参数”、“视频”、“音频”、“源”, ‘画布’、‘轨道’、‘地图’、‘区域’、‘a’、‘em’、‘强’、‘i’、‘b’、‘u’、‘s’、‘小’、‘缩写’、‘q’, ‘cite’、‘dfn’、‘sub’、‘sup’、‘time’、‘code’、‘kbd’、‘samp’、‘var’、‘mark’、‘bdi’、‘bdo’、‘ruby’, “rt”、“rp”、“span”、“br”、“wbr” ]; RSS.prototype.load=函数(回调){ var apiProtocol='http'+(this.options.ssl's':''); var apiHost=apiProtocol+':/'+this.options.host; var apiUrl=apiHost+'?callback=?&q='+encodeURIComponent(this.url); //如果已设置偏移,则将限制设置为偏移结束 if(this.options.offsetStart&&this.options.offsetEnd){ this.options.limit=this.options.offsetEnd; } if(this.options.limit!==null){ apirl+='&num='+this.options.limit; } if(this.options.key!==null){ apirl+='&key='+this.options.key; } $.getJSON(apiUrl,回调); }; RSS.prototype.render=函数(){ var self=这个; 加载(函数(数据){ 试一试{ self.feed=data.responseData.feed; self.entries=data.responseData.feed.entries; }捕获(e){ self.entries=[]; self.feed=null; 返回self.options.error.call(self); } var html=self.generateHTMLForEntries(); self.target.append(html.layout); 如果(html.entries.length!==0){ if($.isFunction(self.options.onData)){ self.options.onData.call(self); } self.appendEntriesAndApplyEffects($('entries',html.layout),html.entries); } 如果(self.effectQueue.length>0){ self.executeEffectQueue(self.callback); }else if($.isFunction(self.callback)){ self.callback.call(self); } }); }; RSS.prototype.appendEntriesAndApplyEffects=函数(目标,条目){ var self=这个; $.each(条目,函数(idx,条目){ var$html=self.wrapContent(条目); 如果(self.options.effect==='show'){ target.before($html); }否则{ $html.css({display:'none'}); target.before($html); applyEffect($html,self.options.effect); } }); target.remove(); }; RSS.prototype.generateHTMLForEntries=函数(){ var self=这个; var result={entries:[],layout:null}; $(this.entries)。每个(函数(){ var分录=此; var offsetStart=self.options.offsetStart; var offsetEnd=self.options.offsetEnd; var评估字符串; //所需偏移量 如果(偏移开始和偏移结束){ 如果(索引>=offsetStart&&index)创建一个周围的div 返回$(''+内容+''); }否则{ //内容包含html=>请勿触摸它 返回$(内容); } }; RSS.prototype.applyEffect=函数($element,effect,callback){ var self=这个; 开关(效果){ 案例“幻灯片”: $element.slideDown('slow',回调); 打破 案例“slideFast”: $element.slideDown(回调); 打破 案例“slideSynced”: self.effectQueue.push({element:$element,effect:'slide'}); 打破 案例“slideFastSynced”: self.effectQueue.push({element:$element,effect:'slideFast'}); 打破 } }; RSS.prototype.ExecuteeEffectQueue=函数(回调){ var self=这个; this.effectQueue.reverse(); var executeEffectQueueItem=函数(){ var item=self.effectQueue.pop(); 如果(项目){ self.applyEffect(item.element、item.effect、executeEffectQueueItem); }否则我