Angularjs ng-view not displaying data

I am learning Angularjs from Tuts + Easier Apps for JavaScript using the AngularJS tutorial. Now I'm in the part where they discussed Routing Primer using ng-view. I am trying to show the contents of a list page in the ng-view of an index page. For some reason, nothing is displayed when loading index.html. I found that from angular 1.2.0 ngRoute has been moved to its own module. I have to download it separately and declare the dependency. But still I canโ€™t show anything on the list page.

index.html

<!doctype html>
<html ng-app="myApp">
  <head>
  <title>Angular App</title>
    <script src="js/angular.min.js"></script>
    <script src="js/angular-route.min.js"></script>
    <script src="app.js"></script>
  </head>
  <body>
    <div ng-controller="Ctrl">
      <div ng-view></div>
    </div>
  </body>
</html>

list.html

<h3>List</h3>
<ul>
    <li ng-repeat="contact in contacts">
      <a href="#">{{contact.name}}</a>: {{contact.number}}
    </li>
</ul>

app.js

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

    app.config(function($routeProvider) {
        $routeProvider.when('/', {
            templateUrl: 'list.html',
            controller: 'Ctrl'
        });
    });

    app.controller('Ctrl', function($scope){
        $scope.contacts = [
            { name: 'Shuvro', number: '1234' },
            { name: 'Ashif', number: '4321' },
            { name: 'Anik', number: '2314' }
        ];
    });
+4
source share
1 answer

Remove ng-controllerfrom the div as follows:

<body>
<div >
  <div ng-view></div>
</div>
</body>

" ", otherwise , : otherwise({ redirectTo: '/'});

app.config(function($routeProvider) {
        $routeProvider.when('/', {
            templateUrl: 'list.html',
            controller: 'Ctrl'
        }).otherwise({ redirectTo: '/'});
});

-

+5

Source: https://habr.com/ru/post/1545553/


All Articles