A
A
Alex2016-05-09 19:12:04
Angular
Alex, 2016-05-09 19:12:04

Where to store data when working with angular.js?

I'm starting to learn angular.js and can't figure out where to store the data.
I have an array like:

.controller('myCtrl', function ($scope) {
    $scope.links = [{
        url: 'http://',
        ancor: 'some string',
    }];
}

and View in which this array is compiled into the menu:
<nav>
    <a ng-repeat="link in links" ng-href="{{link.url}}">{{link.ancor}}</a>
</nav>

This menu is static ( not loaded by ajax). The crux of the matter is: where do I place this array? Directly in the Controller? But after all, according to MVC, data should be stored separately from the controller . Then where should they be? And how to transfer to the controller?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sim3x, 2016-05-09
@sim3x

In the service
stackoverflow.com/questions/18036629/inject-a-conf...
Everything that is not a directive, not a controller goes to the service

D
Dmitry Eremin, 2016-05-09
@EreminD

You can, of course, put it in a separate file, for example. But you, probably, will pull everything from the server, right?

.controller('myCtrl', function ($scope) {
    $scope.links = linksList;
}

var linksList = [{
        url: 'http://',
        ancor: 'some string',
    },
    {
        url: 'http://2',
        ancor: 'some string2',
    }
];

A
Arthur, 2016-05-09
@astralo

maybe try the factory?
in the same place it will be possible to enter ajax sources

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question