27

I'm new to AngularJS and ui-router as well. I try to build an app following a pattern I have seen in a tutorial, but it doesn't seem to work. I inserted two alert statements but they don't run.

projectlist.html is not displayed either.

No errors on JS console.

What is the problem?

JS:

var EntityEditorApp = angular.module('EntityEditorApp', ['ngResource', 'ui.router'])
    .config(function ($stateProvider, $urlRouterProvider) {
        $urlRouterProvider.otherwise('/');

        $stateProvider
            .state('/', {
                url: '/api/Projects',
                templateUrl: 'projectlist.html',
                controller: 'ListCtrl'
            });

    });

EntityEditorApp.factory('Project', function ($resource) {
    alert(1); // This does not run
    return $resource('/api/Project/:id', { id: '@id' }, { update: { method: 'PUT' } });
});

var ListCtrl = function ($scope) {
    alert(1); // This does not run
    $scope.projects = [];
    $scope.search = function () {
        Project.query(function (data) {
            $scope.projects = $scope.projects.concat(data);
        });
    };

    $scope.search();
};

HTML:

<!DOCTYPE html>
<html ng-app="EntityEditorApp" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="Scripts/jquery-1.10.2.js"></script>
    <script src="Scripts/bootstrap.js"></script>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/angular-route.js"></script>
    <script src="Scripts/angular-resource.js"></script>
    <script src="Scripts/AngularUI/ui-router.js"></script>
    <script src="Scripts/app/entityeditor.js"></script>
    <link href="Content/bootstrap.css" rel="stylesheet" />
    <title>AngularJS Tutorial Todo App</title>
</head>
<body>
    <div class="container">
        <div ng-view></div>
    </div>
</body>
</html>

1 Answer 1

82

You need to use ui-view not ng-view

Also, otherwise() takes a URL not a route name. So in your case it should be:

$urlRouterProvider.otherwise('/api/Projects');
Sign up to request clarification or add additional context in comments.

2 Comments

VERY subtle difference. Cost me 30 min. Shouldn't there be an error thrown?
I've got what seems to be the same problem but I'm using ionic, and the only relevant component seems to be: <ion-nav-view> Should I make this another question?

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.