Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs routing not working

Everything worked fine until I tried to add routing. I read that Angularjs version 1.2+ requires 'ngRoute' as an dependency (I am using version 1.2.16). I added it but it still doesn't work. Below are my codes.

test.html (Main Page)

<html ng-app="demoApp">
<head>
    <title></title>
</head>
<body>
    <p>Front Page</p>
    <div ng-view></div>
    <script src="angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-route.js"></script>
    <script src="testjs.js"></script>
</body>
</html> 

testjs.js

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

demoApp.config(function ($routeProvider) {

    $routeProvider.when('/', {
        controller: 'SimpleController',
        templateUrl: '/partials/first.html'
    });

});

var controllers = {};
controllers.SimpleController = function ($scope){
    $scope.first = "Info";
    $scope.customers=[
        {name:'jerry',city:'chicago'},
        {name:'tom',city:'houston'},
        {name:'enslo',city:'taipei'}
    ];
};
demoApp.controller(controllers);

first.html

<div>
    <input type="text" ng-model="name"/>
    </br>
    {{first}}
    </br> 
    <ul>
        <li ng-repeat="cust in customers | filter:name">{{cust.name | uppercase}} - {{cust.city}}</li>
    </ul>   
</div>
like image 614
yellowbyte Avatar asked Apr 17 '14 23:04

yellowbyte


2 Answers

Here is the most basic setup possble, I'll try to make another one with your code: http://plnkr.co/edit/sN9TagVBOdX3mkrxaTiu?p=preview

EDIT updated with the sample code. Everything seems to be working?

EDIT 2 the problem is OP wasn't running a webserver. Ng-Route needs a webserver to function properly.

like image 123
Yoeri Avatar answered Oct 22 '22 14:10

Yoeri


My routing wasn't working because there was an exclamation point inserted in the url when I tried to navigate to my routes. I added $locationProvider like this

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

to remove the exclamation point and my template views started appearing when I navigated to them. I found the answer here Exclamation mark after hash (#!) in angularjs app

like image 26
gary69 Avatar answered Oct 22 '22 13:10

gary69