简体   繁体   English

处理角度模板文件中的标签的脚本不起作用

[英]Script that handles a tag that is within a angular template file does not work

I have this giant code, which is not necessary show here, but he will manipulate this div: 我有这个巨大的代码,这里没有必要显示,但是他会操纵这个div:

<div data-calendar="true"></div>

Finding like this: 找到这样的:

var calendar = $('[data-calendar]');

The script works if the tag is within my main angular layout file, but when the tag is within a angular template file, the script does not work at all. 如果标签在我的主要角度布局文件中,则该脚本起作用,但是当标签在角度模板文件中时,该脚本根本不起作用。

How fix that? 如何解决? I already put this script after the angular scripts, in order to make the angular scripts run before, but didn't work at all. 我已经将此脚本放在角度脚本之后,以使角度脚本之前运行,但根本不起作用。

Including the script in the template is never going to work, you need to be able to control when it runs so that it will run any time that div is added, replaced, etc which could happen any time a digest happens. 包括模板中的脚本将永远无法使用,您需要能够控制其运行时间,以便它在添加,替换div等任何摘要发生的任何时间运行。

The best way to do that in this case would be with a directive. 在这种情况下,最好的方法是使用指令。

<div calendar></div>
.directive('calendar', function() {
  return {
    link: function (scope, element) {
        element.jqCalendar(); // jQuery plugin should be avail here, if you included jQuery and the plugin before angular.
    }
  };
});

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

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