D
D
drumserLance2015-10-07 22:52:52
Angular
drumserLance, 2015-10-07 22:52:52

How to make ng-repeat work inside ng-view?

There is this code:

var app = angular.module('timetableApp', ['ngRoute']);

app.config(['$routeProvider', '$locationProvider',
  function($routeProvider, $locationProvider) {
    $routeProvider
      .when('/test', {
        templateUrl: 'js/templates/test.html',
        controller: 'testCtrl',
      })
      .when('/', {
        templateUrl: 'js/templates/schedule.html',
        controller: 'indexCtrl',
      });

    $locationProvider.html5Mode(true);
}]);

app.controller('testCtrl', function($scope) {
//...
});

app.controller('indexCtrl',function($scope, $http) {
//...
}

In index.html:
<!-- ... -->
<div ng-view class="col-lg-8">

</div>
<!-- ... -->

schedule.html:
<div ng-repeat="lesson in scheduleArray" class="panel panel-default">
    <div class="panel-body">
        # {{lesson.num_lsn}} <br/>
        Дисциплина: {{lesson.name}} <br/>
        Преподаватель: {{lesson.short_fio}} ({{lesson.degree}}) <br/>
        Корпус: {{lesson.corps}} <br/>
        Аудитория {{lesson.classroom}} <br/>
    </div>
</div>

ng-repeat does not work in this case. What could be the problem? Thank you.
IndexControl:
app.controller('indexCtrl',function($scope, $http) {

  $scope.faculties = [];
  $scope.selectedFaculty = null;


  $scope.groups = [];
  $scope.selectedFaculty = null;

  $scope.isFilled = false;

  $scope.isFacultyActive = true;
  $scope.isCourseActive = false;
  $scope.isGroupActive = false;
  $scope.isDateActive = false;
  $scope.isSubmitActive = false;

  $scope.scheduleArray = [];

  $scope.onFacultyChange = function() {
    $scope.selectedCourse = null;
    $scope.selectedGroup = null;
    $scope.selectedDate = null;

    $scope.isCourseActive = false;
    $scope.isGroupActive = false;
    $scope.isDateActive = false;

    if ($scope.selectedFaculty) {
      $scope.isCourseActive = true;
    }
  }

  $scope.onCourseChange = function() {
    $scope.selectedGroup = null;
    $scope.selectedDate = null;

    $scope.isGroupActive = false;
    $scope.isDateActive = false;

    if ($scope.selectedCourse) {
      $scope.isGroupActive = true;
    }
  }

  $scope.onGroupChange = function() {
    $scope.selectedDate = null;

    $scope.isDateActive = false;

    if ($scope.selectedGroup) {
      $scope.isDateActive = true;
    }
  }

  $scope.onDateChange = function() {
    //TODO: Найти лучшее решение
    var comp = $scope.selectedDate.split('/');
    var m = parseInt(comp[0], 10);
    var d = parseInt(comp[1], 10);
    var y = parseInt(comp[2], 10);
    var date = new Date(y,m-1,d);
    if (date.getFullYear() == y && y >= 2000 && (m > 0 && m <= 13) && (d > 0 && d <= 31)) {
        $scope.isSubmitActive = true;
    } else {
        $scope.isSubmitActive = false;
    }
  }

  
  $scope.loadGroups = function() {
    $scope.groups = [];
    $scope.selectedGroup = null;
    $scope.method = 'POST';
      $scope.url = 'ajax/getGroupOfFaculty.php';
      $scope.codeStatus = "";

      var data = {
        	faculty_id : $scope.selectedFaculty,
        	course : $scope.selectedCourse,
        };

    $http({
          method: $scope.method, 
          url: $scope.url,
          headers: {'Content-Type': 'application/x-www-form-urlencoded'},
          data : $.param(data)
      }).
      success(function(response) {
      	if (response != 'null') {
            var groupsArray = JSON.parse(JSON.stringify(response));
            for (var i = 0; i < groupsArray.length; i++) {
            	$scope.groups.push({id: groupsArray[i]['id'], name: groupsArray[i]['name']});
            }
      	}

      }).
      error(function(response) {
          $scope.codeStatus = response || "Request failed";

      });
  }


  $scope.searchSchedule = function() {
    $scope.method = 'POST';
      $scope.url = 'ajax/searchSchedule.php';
      $scope.codeStatus = "";

      var data = {
        	group : $scope.selectedGroup,
        	date : $scope.selectedDate
        };

    $http({
          method: $scope.method, 
          url: $scope.url,
          headers: {'Content-Type': 'application/x-www-form-urlencoded'},
          data : $.param(data)
      }).
      success(function(response) {
    		$scope.scheduleArray = [];
      	if (response && response !='null') {
            var scheduleArray = JSON.parse(JSON.stringify(response));
            for (var i = 0; i < scheduleArray.length; i++) {
            	$scope.scheduleArray.push(scheduleArray[i]);
            }
      	}

      }).
      error(function(response) {
          $scope.codeStatus = response || "Request failed";

      });
  }

  $scope.getFaculties = function() {
      $scope.method = 'POST';
      $scope.url = 'ajax/getFaculties.php';
      $scope.codeStatus = "";

    $http({
          method: $scope.method, 
          url: $scope.url,
          headers: {'Content-Type': 'application/x-www-form-urlencoded'}
      }).
      success(function(response) {
          var facultiesArray = JSON.parse(JSON.stringify(response));
          for (var i = 0; i < facultiesArray.length; i++) {
          	$scope.faculties.push( { id: facultiesArray[i]['id'], name: facultiesArray[i]['name'] });
          }


      }).
      error(function(response) {
          $scope.codeStatus = response || "Request failed";

      });
        
  }
  
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-10-08
@drumserLance

problem with scope variable scheduleArray. Without seeing the controller code, nothing can be said.
Well, yes ... traditionally I repeat - do not use $scope in the controller, use controllerAs. Don't use individual controllers, only in the context of directives. And then you will have a little happiness and less pain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question