简体   繁体   English

使用自定义angularjs指令加载脚本

[英]Loading a script using a custom angularjs directive

I'm very new to angularjs. 我对angularjs很新。 I want to load a script tag using a custom angularjs directive. 我想使用自定义angularjs指令加载脚本标记。 Below is the example code for what I want to achieve: 下面是我想要实现的示例代码:

angular.module('app')
  .directive('myScript', function () {
    return {
      restrict: 'E',
       template : function(e,a){
           return '<script>document.write(5 + 6);</script>';  
        }
    };
  });

So, 11 will be displayed when the directive is added in html: 因此,当在html中添加指令时,将显示11:

<div><my-script></myscript></div>

But I couldn't manage to achieve that. 但我无法实现这一目标。 So, I guess my code is wrong? 那么,我猜我的代码是错的? Please help me with this. 请帮我解决一下这个。 Thanks in advance. 提前致谢。

All right, why would you like to do it by a script tag, Yin? 好吧,你为什么要用脚本标签来做呢,尹?

You can you a built-in directive to do this. 您可以使用内置指令来执行此操作。 Something like ng-bind or even curly braces {{}} AngularJs will evaluate your expression and set the tag value as the result. ng-bind甚至大括号{{}}之类的东西 AngularJs会评估你的表达式并将标记值设置为结果。

Example: <span ng-bind="5 + 6"></span> 示例: <span ng-bind="5 + 6"></span>

Result: <span>11</span> 结果: <span>11</span>

Whenever you need to evaluate an expression you can do this way :) 每当你需要评估表达式时,你都可以这样做:)

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

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