简体   繁体   English

如何获得<script> to share data to Angular Controller?

[英]How to get <script> to share data to Angular Controller?

I am using in unison ASP.NET (aspx web forms) with C# and AngularJS. 我在带有C#和AngularJS的统一ASP.NET(aspx Web表单)中使用。 I have some script in my index.html file: 我的index.html文件中有一些脚本:

<script>
    function get_stuff(){
       var stuff = document.getElementById("stuff_here").value;
       console.log(stuff);
       // output: [{"name":"toy duck", "color":"yellow"},{"name":"flute", "color":"white"}]
    }
</script>
<input type='hidden' id='stuff_here' value='...' />
<button onclick="get_stuff()">RUN</button>

The output is as desired. 输出是所需的。 However, I want to be able to access the output from my Angular controllers, such as using ng-click on the button and having an angular function do the same stuff. 但是,我希望能够访问我的Angular控制器的输出,例如使用ng-click按钮并让angular函数做同样的事情。 Unfortunately ng-click does not work when the button is pressed. 不幸的是,当按下按钮时,ng-click不起作用。 I have also tried putting var stuff as a global variable outside the function, but my Angular functions cannot pick up it either. 我也曾尝试将var东西作为全局变量放置在函数之外,但是我的Angular函数也无法使用它。

How can I get the object?? 我如何得到物体? Thanks in advance for the help! 先谢谢您的帮助!

You can hold bootstrapped application data as an Angular service or, to be more exact, an Angular provider. 您可以将引导应用程序数据保存为Angular服务,或更确切地说,将其保存为Angular提供程序。

<script>
    var stuff = document.getElementById("stuff_here").value;
    angular.module("myApp").value("myProviderStuff", stuff);
</script>

Then within your angular app, 然后在您的角度应用中

var module = angular.module("myApp");
module.controller("SomeController", function($scope, myProviderStuff) {
$scope.stuff = myProviderStuff;

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

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