简体   繁体   English

角度:从内部访问范围<script> tag

[英]Angular: Access scope from within <script> tag

I have a scope variable that is being populated from my API. 我有一个从我的API填充的范围变量。 I can access this from HTML with the {{}} braces. 我可以使用{{}}大括号从HTML进行访问。

I need to pass the scope into a script file. 我需要将范围传递到脚本文件中。

<script type="text/javascript"> var data = {{data}}  </script>

From what i've seen so far it seems overly complex for something relatively simple. 从到目前为止我所看到的,相对简单的东西似乎过于复杂。

What is the recommended way of doing this? 建议这样做的方法是什么? Isn't this quite a common problem? 这不是一个普遍的问题吗?

Specifically, im trying to use a javascript graphical library Cytoscape.js. 具体来说,我正在尝试使用JavaScript图形库Cytoscape.js。 The graph is populated from a javascript object. 该图是从javascript对象填充的。 I'd like to populate this javascript object from the scope. 我想从范围中填充此javascript对象。 I found this: 我找到了这个:

Pass Angular scope variable to Javascript 将Angular作用域变量传递给Javascript

I'm wondering whether generally this is handled in a different way in angular as it seems like a hack. 我想知道通常是否以不同的方式处理角度问题,就像是hack。

First of all to be clear, the Angular does not perform interpolation within script tags. 首先要明确的是,Angular不会在脚本标签内执行插值。

https://docs.angularjs.org/guide/interpolation https://docs.angularjs.org/guide/interpolation

There are some ways around this... calling directly the value of the variable can be one of the ways: 有一些解决这个办法...调用变量的直接值可以是途径之一:

<body ng-app="myApp">
    <div ng-controller="myController" id="yourControllerElementID">

    </div>
</body>

<script type="text/javascript">
    var app = angular.module("myApp", []);

    app.controller("myController", myController);

    function myController($scope){
        $scope.data = 'some value';
    }

    // Capturing value from outside the controller
    setTimeout(function(){
        var data = angular.element(document.getElementById('yourControllerElementID')).scope().data;
    }, 1000);
</script>

Otherwise using interpolation as you wanted , would create a hidden input with the value of the controller and capture this value with vanilla javascript or jquery: 否则,根据需要使用插值,将使用控制器的值创建隐藏的输入,并使用香草javascript或jquery捕获此值:

<body ng-app="myApp">
    <div ng-controller="myController">
        <input type="hidden" id="myValueFromController" value="{{data}}" />
    </div>
</body>

<script type="text/javascript">
    var app = angular.module("myApp", []);

    app.controller("myController", myController);

    function myController($scope){
        $scope.data = 'some value';
    }

    // Capturing value from outside the controller
    setTimeout(function(){
        var data = document.getElementById("myValueFromController").value;  // or
        var data = $("#myValueFromController").val();
    }, 1000); 
</script>

Anyway let's say it would not be the Angular Way to work , but as I do not know what the scope of its development, this is the best I can tell you. 不管怎么说,这不是Angular的工作方式,但是由于我不知道它的开发范围,这是我能告诉您的最好的方法。

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

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