0

I'm using angularjs 1.6 to create a simple app just to display a list of groups and to display all users for each group ... my problem is I can't display the list of users using ng-view

this is my index.html :

<!DOCTYPE html>
<html lang="en" ng-app="application"> 
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>My application</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="assets/bootstrap-3/css/bootstrap.min.css">
  <link rel="stylesheet" href="assets/app.css">
</head>
<body>
  <div ng-controller="groupctrl">
    <div class="well">
      <ul ng-repeat="grp in group">
        <li class="link" ng-click="displayusers(grp.uuid)">{{ grp.name }}</li>
        <li class="link" ng-click="displayusers(grp.uuid)">{{ grp.name }}</li>
     </ul>
    </div>
  </div>
    <div ng-view>

    </div>
</body>
  <script src="assets/angular.min.js"></script>
  <script src="assets/angular-route.min.js"></script>
  <script src="controllers/app.js"></script>
  <script src="controllers/route.js"></script>
  <script src="controllers/service.js"></script>
  <script src="controllers/mainctrl.js"></script>

</html>

app.js

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

route.js and mainctrl.js

app.config(['$locationProvider', '$routeProvider', 
 function($locationProvider, $routeProvider) {

    $locationProvider.hashPrefix('!');

    $routeProvider
    .when('/', {
      templateUrl: 'index.html'
     })
    // .when('/group', {
    //    templateUrl: 'group.html',
    //    controller: 'groupctrl'
    //  })
     .when('/users', {
       templateUrl: 'users.html',
       controller: 'usersctrl'
     })
    .otherwise({redirectTo: '/'});
}]);

app
.controller('groupctrl', function($scope, $timeout, $location, apiService) {

$scope.hello = "hello 123";

apiService.getgroup().then(function(data){
 $scope.group = data;
});

$scope.displayusers = function(idgroup){
 apiService.getusers(idgroup);
 $location.path('/users');
};

})
.controller('usersctrl', function($scope, $timeout, apiService) {


apiService.getusers().then(function(data){
 $scope.users = data;
});

});

users.html

<div ng-controller="usersctrl">
    <div ng-repeat="user in users">
      <div class="well text-center">
         <h2>{{ user.name }}</h2>
         <h5>{{ user.index }}</h5>
             <button class="btn btn-success" ng-click="edit(user.index)">EDIT</button>
             <button class="btn btn-danger" ng-click="delete(user.index)">DELETE</button>
      </div>
    </div>
  </div> 

thank's for helping !

ghazi
  • 59
  • 1
  • 9
  • Please describe your problem more precisely, what happens when you navigate to `/users`? Where does it redirect and is there any error in browser console? Which browser did you checked your code on? – Jakub Licznerski Jan 15 '18 at 15:01
  • I'm using chrome and Ubuntu , my problem is I can't display the users.html inside index.html by ng-view !! – ghazi Jan 15 '18 at 15:12
  • Open browser console (F12) and redo. Do you notice any errors in the console? If yes, paste them in your question. If no, does the url change to `/users/`? – Jakub Licznerski Jan 15 '18 at 15:18
  • ok, the only notice is: XMLHttpRequest cannot load file:///home/folder-test/Documents/application/users.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. – ghazi Jan 15 '18 at 15:23

1 Answers1

0

Referring to comments provided by the question's author, the problem is with accessing to the users.html file as prompted in error:

XMLHttpRequest cannot load file:///home/folder-test/Documents/application/users.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https

This problem is resolved in other SO questions such as this and more specifically for AngularJS here.

Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33
  • thant's not my problem, I'm talking about routing and ng-view !! to display users template !! – ghazi Jan 15 '18 at 15:32
  • did you place `users.html` exactly where `index.html` is placed (most probably, `public` folder)? – Jakub Licznerski Jan 15 '18 at 15:39
  • Looking at the error again I assume that you are just opening `index.html` locally. This way you will not be able to access any resources not defined in `index.html` file (e.g. other local html files). Look [here](https://stackoverflow.com/questions/29528922/how-to-create-a-localhost-server-to-run-an-angularjs-project) or [here](https://www.npmjs.com/package/http-server) for info how to deploy angularjs on simple web server. – Jakub Licznerski Jan 15 '18 at 15:46