0

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?

1
  • You can put the arrays in services, sure. You can also put them in, say, a text file and fetch them with ajax when needed. Commented Nov 26, 2015 at 12:56

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Nice one, did not know about constant in Angular.
2

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

3 Comments

How about a Uint8Array?.. My array is actually the binary firmware file for an embedded device. It looks like this now: var firmware = new Uint8Array([0x10, 0x20, 0x30 ......... ]).
Just put it instead of [{ name: "Item 1" },...]
@Jolle i've updated my code to fit more to your situation.
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

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

2 Comments

Can the path be a local one? I am developing for the Ionic App framework, and the arrays has to be local in the app?
yes the path can be local anywhere in your directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.