有没有办法将数据加载到脚本中

时间:2015-04-26 05:31:39

标签: javascript jquery angularjs

所以在Angular我正在尝试

$scope.data = "<script> alert('hi'); </script>";

但不幸的是,这不起作用。我还尝试添加 ng-bind-html ,但没有任何结果。

{{data}}

我也尝试在脚本标记中加载数据,但似乎也没有用。有没有办法避免这一切?例如

$scope.data = "bob";

-

 <script>
    var name = {{data}};
    </script>

2 个答案:

答案 0 :(得分:1)

您可以创建一个指令,将脚本动态加载到DOM中。

<强>标记

app.directive('loadScript', function($compile){

  return {
    restrict: 'E',
    scope: {
      'data': '='
    },
    link: function(scope, element, attrs){
      element.append($compile(scope.data)(scope))
    }
  }
})

<强>指令

DirectoryIndex app.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /app.php/
    </IfModule>
</IfModule>

Working Plunkr

答案 1 :(得分:0)

您的<script>已经在alert块或Javascript文件中。

现在,您希望何时/如何调用<div ng-click="doAlert()"> Click here to see an alert </div> ?如果我理解你正在尝试做什么,请按照以下步骤操作:

$scope.doAlert = function() {
   alert('hi);
};

并在您的控制器中:

MyObject obj1 = new MyObject();
MyObject obj2 = new MyObject();
MyObject obj3 = new MyObject();
MyObject obj4 = new MyObject();

values[obj1][obj2] = 1;
values[obj2][obj4] = 2;
values[obj1][obj4] = 3;

int someValue = values[obj1][obj2];
int someOtherValue = values[obj2][obj4];
int anotherValue = values[obj1][obj4];