1

I am studying AngularJS and met the problem with my, actually, hello world app. At first I will show the code:

index.html

<!DOCTYPE html>
<html ng-app="nazwa">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<a href = "#">Add Order</a>
<div>
    <div ng-view></div>
</div>

<script src="scripts/angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>

app.js

var mainModule = angular.module("nazwa", []);

mainModule.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);
    $routeProvider
        .when('/', {
            controller: 'customerController',
            templateUrl: 'partials/View1.html'
        })
        .when('/cos', {
            controller: 'customerController',
            templateUrl: 'partials/View2.html'
        })
        .otherwise({
            redirectTo: '/'
        });
}]);


mainModule.controller('customerController', function ($scope) {
    $scope.customers = [
        {name: 'john', lastname: 'dersky'},
        {name: 'someone', lastname: 'lastname'},
        {name: 'peter', lastname: 'dewski'}
    ];

    $scope.addCust = function () {
        $scope.customers.push(
            {name: $scope.newCust.name, lastname: $scope.newCust.lastname}
        )
    }
});

View1.html

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

    <br/>
    Customer Name:<br/>
    <input type="text" ng-model="newCust.name"/><br/>
    Customer Lastname:<br/>
    <input type="text" ng-model="newCust.lastname"/><br/>
    <button ng-click="addCustomer()">Add</button>
    <br/>
    <a href="#/cos">View 2</a>
</div>

And View2.html is pretty the same, but few changes

And the problem is that when I run index.html nothing shows. Even if I add some {{1+2}} code to index.html, the browser shows raw "{{1+2}}" rather than "3". When I comment whole routing (mainModule.config) then {{1+2}} works fine and shows 3, but index.html still show neither View1 nor View2

What causes the problem? I have searched whole web, for hours and nothing.. what a shame

thanks for you help and dont beat me too hard for my mistakes, I am quite new in Angular :)

edit: I found the console log errors: http://i57.tinypic.com/2e5qgeh.jpg enter image description here

2 Answers 2

3

As dfsq mentioned you are most likely missing ngRoute module. It is available for download here.

Include that in your project before app.js as well as the code changes that dfsq mentioned.

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

7 Comments

thank you so much! it is the answer :) although another problem is that when I press "Add" button to add new customer, then nothing happens, what can be the problem?
@azalut you didn't connect your button to the function in your scope. Add ng-click="addCust()" to your Add Order link
omg such a stupid mistake.. though experience in programming still making such a mistakes :) thank you doubly
@azalut No problem! Feel free to mark my answer as correct if you'd like to repay me :P
here you go ;)I was following the video-tutorial on youtube and the autor didnt have to include any angular-route JS file, strange.. maybe it depends on angularjs version?
|
3

You need to add ngRoute module dependency:

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

By the way open a console (F12) and check for errors: there must be one saying that.

5 Comments

I tried that but still doesn't work, any other ideas? I found the errors in console, pic is upper
Are you really posting complete code? Because error states that there something else except ngRoute error.
yup, the files I posted are ctr+a, ctrl+c, ctrl+v :) I had only changed mainModule name from "nazwa" to "myApp" but now it is changed again to "nazwa" (read the code in my question once more)
@azalut Its not enough to just add ['ngRoute'] to your angular module. You need to actually download the js for ngRoute and include that on your page
@JesseCarter Damn, totally true! Missed that :) Thank you for valuable note!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.