Reputation: 1426
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.
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?
Upvotes: 0
Views: 2480
Reputation: 8423
Just to elaborate on @hansmaad's answer and provide a demo
HTML
<div ng-app="myApp" ng-controller="myController">
<div ng-repeat="item in bigArray">
<h1>{{item.name}}</h1>
</div>
</div>
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
HTML
<div ng-app="myApp" ng-controller="myController">
<div ng-repeat="item in bigArray">
<h1>{{item}}</h1>
</div>
</div>
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
Upvotes: 1
Reputation: 804
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
$http.get('/path to your file')
.success(function(data){
//logic
})
.error(function(data){
//capture error
});
Upvotes: 0
Reputation: 18915
You could choose one of the provider recipes to inject the array into controllers, services or directives.
https://docs.angularjs.org/guide/providers
The constant recipe would be a good start
myApp.constant('bigArray', myArray);
Upvotes: 3