AngularJS如何从其他文件中包含数组?

时间:2021-12-24 15:05:04

I have some rather large binary (200kb) arrays defined inside a controller. Now I want to put these large arrays in seperate files, and somehow include them or get hold of them in my controller.

我在控制器中定义了一些相当大的二进制(200kb)数组。现在我想将这些大型数组放在单独的文件中,并以某种方式包含它们或在我的控制器中获取它们。

Is there an elegant way of doing this? Could I maybe put the arrays in different services, and then get the arrays from the services?

这样做有一种优雅的方式吗?我可以将数组放在不同的服务中,然后从服务中获取数组吗?

3 个解决方案

#1


1  

Just to elaborate on @hansmaad's answer and provide a demo

只是详细说明@hansmaad的答案并提供演示

HTML

HTML

<div ng-app="myApp" ng-controller="myController">
    <div ng-repeat="item in bigArray">
        <h1>{{item.name}}</h1>
    </div>
</div>

JavaScript

JavaScript的

var app = angular.module("myApp", []);

//In another file
app.constant("BigArray", [
    {
        name: "Item 1"
    },
    {
        name: "Item 2"
    },
    {
        name: "Item 3"
    }
]);
//In another file end

app.controller("myController", ["$scope", "BigArray", function($scope, BigArray){
    $scope.bigArray = BigArray; 
}]);

UPDATE

UPDATE

HTML

HTML

<div ng-app="myApp" ng-controller="myController">
    <div ng-repeat="item in bigArray">
        <h1>{{item}}</h1>
    </div>
</div>

JavaScript

JavaScript的

var app = angular.module("myApp", []);

//In another file
app.constant("BigArray", new Uint8Array([0x10, 0x20, 0x30]));
//In another file end

app.controller("myController", ["$scope", "BigArray", function($scope, BigArray){
    $scope.bigArray = BigArray; 
}]);

Updated JSFiddle

更新了JSFiddle

#2


3  

You could choose one of the provider recipes to inject the array into controllers, services or directives.

您可以选择一个提供程序配方将阵列注入控制器,服务或指令。

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

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

The constant recipe would be a good start

不断的配方将是一个良好的开端

myApp.constant('bigArray', myArray);

#3


0  

You can categorize your arrays in different service files and inject those services in your controller to get those arrays and then can manage your data in controller file. You can also save those arrays in simple json files and make an http get request to get that data like

您可以在不同的服务文件中对数组进行分类,并在控制器中注入这些服务以获取这些数组,然后可以在控制器文件中管理数据。您还可以将这些数组保存在简单的json文件中,并生成一个http get请求来获取该数据

$http.get('/path to your file')
 .success(function(data){
    //logic
    })
 .error(function(data){
            //capture error
});

#1


1  

Just to elaborate on @hansmaad's answer and provide a demo

只是详细说明@hansmaad的答案并提供演示

HTML

HTML

<div ng-app="myApp" ng-controller="myController">
    <div ng-repeat="item in bigArray">
        <h1>{{item.name}}</h1>
    </div>
</div>

JavaScript

JavaScript的

var app = angular.module("myApp", []);

//In another file
app.constant("BigArray", [
    {
        name: "Item 1"
    },
    {
        name: "Item 2"
    },
    {
        name: "Item 3"
    }
]);
//In another file end

app.controller("myController", ["$scope", "BigArray", function($scope, BigArray){
    $scope.bigArray = BigArray; 
}]);

UPDATE

UPDATE

HTML

HTML

<div ng-app="myApp" ng-controller="myController">
    <div ng-repeat="item in bigArray">
        <h1>{{item}}</h1>
    </div>
</div>

JavaScript

JavaScript的

var app = angular.module("myApp", []);

//In another file
app.constant("BigArray", new Uint8Array([0x10, 0x20, 0x30]));
//In another file end

app.controller("myController", ["$scope", "BigArray", function($scope, BigArray){
    $scope.bigArray = BigArray; 
}]);

Updated JSFiddle

更新了JSFiddle

#2


3  

You could choose one of the provider recipes to inject the array into controllers, services or directives.

您可以选择一个提供程序配方将阵列注入控制器,服务或指令。

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

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

The constant recipe would be a good start

不断的配方将是一个良好的开端

myApp.constant('bigArray', myArray);

#3


0  

You can categorize your arrays in different service files and inject those services in your controller to get those arrays and then can manage your data in controller file. You can also save those arrays in simple json files and make an http get request to get that data like

您可以在不同的服务文件中对数组进行分类,并在控制器中注入这些服务以获取这些数组,然后可以在控制器文件中管理数据。您还可以将这些数组保存在简单的json文件中,并生成一个http get请求来获取该数据

$http.get('/path to your file')
 .success(function(data){
    //logic
    })
 .error(function(data){
            //capture error
});