0

I have been working on this tutorial "AngularJSDemos" Dan Wahlin link https://www.youtube.com/watch?v=i9MHigUZKEM, but the application SPA AngularJS is not rendering on the webpage. Is there anything missing in my codes below?

The Html pages have the following codes: "Using AngularJS Directives and Data Binding"

<!DOCTYPE html>
<html data-ng-app="demoApp"> 
<head>
     <title>Using AngularJS Directives and Data Binding</title>
</head>
<body>
<div>
    <!--Placeholder for views-->
    <div data-ng-view=""></div>
</div>
<script src="Scripts/angular.min.js"></script>

<script>
    var demoApp = angular.module('demoApp', []);

    demoApp.config(function ($routeProvider) {
        $routeProvider
        .when('/view1',
        {
            controller: 'SimpleController',
            templateUrl: 'Partials/View1.html'
        })
        .when('/view2',
        {
            controller: 'SimpleController',
            templateUrl: 'Partials/View2.html'
        })
        .otherwise({ redirectTo: '/view1' });
    });

    demoApp.factory('simpleFactory', function () {
        var customers = [
            { name: 'John Smith', city: 'Phoenix' },
            { name: 'Jane Doe', city: 'San Francisco' },
            { name: 'John Doe', city: 'New York' },
            { name: 'Mike Leski', city: 'Clarksburg' },

        ];

        var factory = {};
        factory.getCustomers = function() {
            return customers;
        };

        factory.postCustomer = function (customer) {

        };
        return factory;
    });

    demoApp.controller('SimpleController', function ($scope, simpleFactory) {
        $scope.customers = [];

        init();

        function init() {
            $scope.addCustomer = simpleFactory.getCustomers();
        }

        $scope.addCustomer = function () {
            $scope.customers.push(
                {
                    name: $scope.newCustomer.name,
                    city: $scope.newCustomer.city
                });
        };
    });

</script>

The View1.html

  <div class="container">
    <h2>View 1</h2>
    Name:
    <br />
    <input type="text" data-ng-model="filter.name" />
<br />
<ul>
    <li data-ng-repeat="cust in customers | filter:filter.name | orderBy:'city'">{{ cust.name | uppercase}} - {{ cust.city | lowercase}}</li>
</ul>

<br />
Customer Name:<br />
<input type="text" data-ng-model="newCustomer.name"/>
<br />
Customer City:<br />
<input type="text" data-ng-model="newCustomer.city" />
<br />
<button data-ng-click="addCustomer()">Add Customer</button>
<br />
<a href="#/view2">View 2</a>
 </div>

And the last one View2.html

  <div class="container">
  <h2>View 2</h2>
  City:
  <br />
  <input type="text" data-ng-model="city" />
  <br />
  <ul>
    <li data-ng-repeat="cust in customers | filter:city | orderBy:'city'">{{ cust.name | uppercase}} - {{ cust.city | lowercase}}</li>
</ul>
  </div>

1 Answer 1

1

The problem is that you are missing the ngRoute dependency when you declare your demoApp module:

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

Here is the working example of your code

Also remember to add the script of ngRoute, you can use:

npm install angular-route --save-dev

Or add the CDN script:

https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-route.js for example

PD: I recommend you to use ui-router is more robust

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.