0

I'm using angularJs and I must fill the array in ng-init with multiple values:

<div ng-init="script = [
            {tag:'tag1', title:'Title1 here', plot:'here goes the plot1'},
            {tag:'tag2', title:'Title2 here', plot:'here goes the plot2'},
            {tag:'tag3', title:'Title3 here', plot:'here goes the plot3'}
            ]">
</div>

as you may imagine, the "plot" field is a string which may be quite big and confusing if I write it there. Is it possible to take it from another file? For example I may make three files called plot1, plot2, plot3 and I may call them into my HTML file

2 Answers2

1

I wouldn't recommend using ng-init. I'd suggest using a view controller and load the script via the following logic.

.controller( 'viewController', function($scope, $http){
     $http.get(<location of file to load>)
     .then( function(rsp){
          $scope.script = rsp
     })
})

If you wanted to load multiple files, then either make multiple requests, or if they are contingent upon one another, use $q.all to make sure you load them all:

$q.all([
    $http.get(<location of file1 to load>),
    $http.get(<location of file2 to load>),
    $http.get(<location of file3 to load>)
])
.then( function( rsps ){
     $scope.script = rsps
})

links:

Community
  • 1
  • 1
jusopi
  • 6,791
  • 2
  • 33
  • 44
0
.controller( 'viewController', function($scope, $http){
     $http.get(url)
     .then( function(response){
          $scope.initVal = response;
     })
})

<div ng-init="initVal ">
</div>
Shashi
  • 1,112
  • 2
  • 17
  • 32