0

I want create a system where a section of code from an external text file is imported and repeated using angular js. The code needs to run through a number of text files and create a new section for each I have been attempting to rewrite my ajax code from a previous problem in angular js and I cannot seem to find a way to make it work. That problem can be found here: Calling angularjs inside.

My old AJAX code:

$.ajax({
    url : '../src/text/section1.txt',
    dataType: 'text',
    success : function (data) {
        $('#section1').append(data);
    },
});

This is what I have currently:

app.controller('sectionController', ['$scope', function($scope, $http) {
  $scope.sections = [
    {id: 1, title: 'Page 1', data: ''},
    {id: 2, title: 'Page 2', data: ''},
    {id: 3, title: 'Page 3', data: ''},
    {id: 4, title: 'Page 4', data: ''},
    {id: 5, title: 'Page 5', data: ''},];

  angular.forEach($scope.sections, function(id) {
    $http({
      url : '../../src/text/section' + $scope.sections.id +'.txt',
      dataType: 'text',
    }).success(function (data) {
      $scope.sections[id].data = data;
    });
  });
}]);

My HTML is:

<div class="body">
            <div class="bodyBackground"></div>
            <div class="contentContainer" ng-controller="sectionController">
                <div id="sectionContainer" ng-repeat="section in sections">
                    <div id="section{{ section.id }}">
                        <h3 class="sectionTitle">{{ section.title }}</h3>
                        <div>{{ section.data }}</div>
                    </div>
                </div>  
            </div>
        </div>

Thanks in Advance

Community
  • 1
  • 1
AidenDawn
  • 5
  • 4
  • Ok? What's the problem? – Evan Davis Oct 25 '15 at 19:26
  • Suggest you study some angular tutorials that show you how to use roting and how to use model data to render the view. `document.querySelector('#section' + id)` is the wrong approach . What is the higher level overview of what a `page` is in your data? And what is in the text files? – charlietfl Oct 25 '15 at 19:51
  • The text files contain text wrapped in

    tags

    – AidenDawn Oct 25 '15 at 21:22

1 Answers1

0

consider adding a 'data' property to your $scope.sections objects

$scope.sections = [
{id: 1, title: 'page 1', data: ''}
]

then in the success of your HTTP set the response data to the appropriate section object data property

  }).success(function (data) {
      section.data = data;
    });

and in your HTML Bind to the data property of the section object

    app.controller('sectionController', ['$scope', '$http', function($scope, $http) {
    $scope.sections = [
        {id: 1, title: 'Page 1', data: ''},
        {id: 2, title: 'Page 2', data: ''},
        {id: 3, title: 'Page 3', data: ''},
        {id: 4, title: 'Page 4', data: ''},
        {id: 5, title: 'Page 5', data: ''}];

    angular.forEach($scope.sections, function(section) {
        $http({
            url : '../../src/text/section' + section.id +'.txt',
            dataType: 'text'
        }).success(function (data) {
            section.data = data;
        });
    });
}]);
sam schonstal
  • 429
  • 2
  • 10
  • I have edited my code to reflect your suggestions but it is still not functional. Have I missed anything? – AidenDawn Oct 25 '15 at 21:42
  • a couple of issues with how you were referencing the section in the forEach, i renamed id to section to be more clear and adjusted the references. also missing annotation for injection of $http – sam schonstal Oct 26 '15 at 01:03