Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将数组值从javascript传递到angularjs控制器_Javascript_Angularjs_Angularjs Scope_Global Variables_Javascript Objects - Fatal编程技术网

如何将数组值从javascript传递到angularjs控制器

如何将数组值从javascript传递到angularjs控制器,javascript,angularjs,angularjs-scope,global-variables,javascript-objects,Javascript,Angularjs,Angularjs Scope,Global Variables,Javascript Objects,我试图传递从JavaScript文件的数组中获取的值,并使用全局变量将其推送到angularJS 在angular文件中,我试图通过访问全局变量获取这些值,如 但它返回我未定义,但我运行 如何在我的控件中访问这些值不是一个好主意,可能您需要与其他web或框架交互 如果您应该使用一个全局变量,您可以使用index.html中的特殊脚本进行声明 <script type="text/javascript"> let globalarray; </script>

我试图传递从JavaScript文件的数组中获取的值,并使用全局变量将其推送到angularJS

在angular文件中,我试图通过访问全局变量获取这些值,如 但它返回我未定义,但我运行


如何在我的控件中访问这些值不是一个好主意,可能您需要与其他web或框架交互

如果您应该使用一个全局变量,您可以使用index.html中的特殊脚本进行声明

<script type="text/javascript">    
 let globalarray; 
</script>

或者$rootScope。

通过上面显示的示例

  • 您没有在
    全局数组中推送值。通过不调用
    abc()
    函数

  • 如果您的
    globalarray
    变量是窗口级全局变量,则可以从angular应用程序中的任何位置获得该变量

  • 请观察我为演示而做的小提琴中的行为。我也做了一个简单的解释,这样你就会明白了

    更新

    由于数据来自回调,因此需要重新运行摘要循环并更新变量。请注意这把小提琴,因为它有你的初始坐标

    // we got the cords
    var cords = ol.proj.transform(coordinates, 'EPSG:3857', 'EPSG:4326');
    
    // we get the scope from the element that has the controller binded to it.
    var scope = angular.element(document.getElementById("MainWrap")).scope();
    
    // we call a digest cycle of angular to update our scope variable sinse it comes in a callback when angular is loaded
    scope.$apply(function () {
       scope.updateCords(cords);
    });
    

    希望现在能有所帮助。

    试试以下方法:

    $scope.globalarray = [];
    
    function abc() {
       var array = [4,5]
       angular.forEach(array,function(k,v){
         $scope.globalarray.push(k); 
      })
    
    }
    

    请查看此链接:

    您可以用另一种方式来考虑,一旦您访问了作用域,您只需使用数组中的数据调用控制器内的setter方法,然后就可以执行任何需要的操作。我在一个项目中使用过这种方法,效果很好

    例如:

    //externalScope是在角度控制器中声明的全局变量 //这是一种从外部访问控制器和触发器方法的方法 //我们所要做的就是向angular发出信号,表示某些数据已经更改,以便它可以做些什么。在我的示例中,selectedUserValue是我传递给控制器的数据位

    if (externalScope) {
        externalScope.$apply(function () {
            externalScope.selectUser(selectedUserValue);
        });
    }
    
    当您尝试使用某些数据调用scope方法时,此代码位于控制器之外

    现在在实际控制器中,我们有如下内容:

        var externalScope;
    
        (function () {
       controller code
    
    控制器中的某个位置:

    externalScope = $scope; //this exposes the scope to the outside world
    
    注意externalScope变量是如何在控制器外部声明的,因此它将是全局的

    在此之后,只需在从外部调用的angular方法中编写所需的代码

    在我的例子中,它是一个setter,然后它调用其他一些使用该数据的东西:

    $scope.selectUser = function (userID) {
                if (userID && userID !== $scope.selectedUserID) {
                    $scope.selectedUserID = userID;
                    $scope.loadUserRecords();
                }
            };
    

    希望这是有意义的。免责声明这不是我所说的琐碎的东西,只有当你真的没有其他选择时才应该使用

    如果变量是全局变量(我的意思是,窗口级别的全局变量),那么它在应用程序中的任何位置都必须可用,包括在控制器内部。首先,abc函数声明错误,您在{@v.josh之前忘记了(),您可以发布吗?因为这样做
    var globalarray=[];函数abc(){var array1=[4,5];globalarray.push(array1)};abc();(function(){console.log(globalarray[0]);}();
    将给出正确的输出:
    [4,5]
    。您的方法完全不正确。您必须用包装OSM的代码。在这种情况下,您将不会遇到问题中提到的问题。我已经尝试过这个$scope.globalarray=$window.globalarray;console.log($scope.globalarray),但仍然未定义。当我检查了console.log(globalarray.length)的长度时,它返回0,但返回console.log(globalarray)把那个还给我values@v.josh请看更新后的答案和随附的提琴。我已经尽可能地描述了。你能检查一下我更新的弹夹吗?然后你就可以确切地知道我在说什么了want@M.JunaidSalaat使用
    scope()
    方法不推荐使用,并且不能与
    $compileProvider.debugInfoEnabled一起使用(false);
    @v.josh你签出这个plunker了吗?实际上我的控制器在javascript文件之前先加载,所以当我运行我的文件时,它给我的值是未定义的。我尝试使用$apply functron,但它仍然不起作用。请检查我更新的plunker。
    externalScope = $scope; //this exposes the scope to the outside world
    
    $scope.selectUser = function (userID) {
                if (userID && userID !== $scope.selectedUserID) {
                    $scope.selectedUserID = userID;
                    $scope.loadUserRecords();
                }
            };