1

I'm trying to build a very simple app using angular UI-Router.

My index.html file:

<!DOCTYPE html>
<html ng-app="elara">
<head>
<script type="text/javascript" src="/bower_components/angular/angular.js"></script>
<script type="text/javascript" src="/bower_components/angular-ui-router/release/angular-ui-router.js"></script>
<script type="text/javascript" src="/js/app.js"></script>
<script type="text/javascript" src="/js/ctrl/indexCtrl.js"></script>
<script type="text/javascript" src="/js/ctrl/registerCtrl.js"></script>
</head>
<body>
    <ng-view>   </ng-view>
</body>
</html>

app.js file

angular.module('elara', ['ui.router'])

.run(
    ['$rootScope', '$state', '$stateParams',
        function($rootScope, $state, $stateParams) {

            // It's very handy to add references to $state and $stateParams to the $rootScope
            // so that you can access them from any scope within your applications.For example,
            // <li ng-class="{ active: $state.includes('contacts.list') }"> will set the <li>
            // to active whenever 'contacts.list' or one of its decendents is active.
            $rootScope.$state = $state;
            $rootScope.$stateParams = $stateParams;
        }
    ]
)

.config(
    ['$stateProvider', '$urlRouterProvider',
        function($stateProvider, $urlRouterProvider) {
            $stateProvider

            .state('home', {
                url: '/',
                templateUrl: '/tmpl/index.html',
                controller: 'indexCtrl'
            })

            .state('register', {
                url: '/register',
                templateUrl: '/tmpl/register/register.tmpl.html',
                controller: 'registerCtrl'
            })
        }
    ]);

The indexCtrl.js file

app = angular.module('elara', []);

app.controller('indexCtrl', ['$scope', function($scope){
    $scope.message = "index"
}])

and registerCtrl.js is same. Just controller name and message is different.

in my x.tmpl.html files i just want to write message variable.

{{message}}

Problem is, when I navigate to url / or #/register I am always getting empty page. message variable is not in the page or my template htmls are not loaded.

Also there are not errors in the console.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Sefa
  • 8,865
  • 10
  • 51
  • 82

1 Answers1

1

The target for states is ui-view

// <ng-view>   </ng-view>
<div ui-view=""></div>

In case, we use named views : {'myName': { ... } } we provide the name like this

<div ui-view="myName"></div>
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • I have changed my ng-view element as ui-view as you show but i still have the same problem. – Sefa Oct 17 '15 at 15:11
  • Missing line to make that all running is also `$urlRouterProvider.otherwise("/");` I created fully working example here http://plnkr.co/edit/Xnncd8g4UQOh0OW9QpkH?p=preview – Radim Köhler Oct 17 '15 at 15:17
  • Here is a bit more complex solution example with kind of parent state and layout views ... http://stackoverflow.com/q/28800644/1679310 it could give some insight into UI-Router named `views` – Radim Köhler Oct 17 '15 at 15:18
  • Thanks for the plunker! I still can't get mine working. I added the missing line and compared our codes. Added 'home' to my index.html file and realized it's not getting "href" property when the page is rendered. Can this give us a clue? – Sefa Oct 17 '15 at 15:29
  • I would suggest.... really take my stuff, into your local. Then, try step by step replace original pieces. Also, observe console log for messages. You are really close... – Radim Köhler Oct 17 '15 at 15:30
  • I'll do that. Thanks for all the info. – Sefa Oct 17 '15 at 15:32
  • Here is even closer plunker to your solution: http://plnkr.co/edit/bnKILSH2mjNwB8AuWgfe?p=preview *(I just removed the starting slash in your src and templateUrl)* – Radim Köhler Oct 17 '15 at 15:37
  • look your last plunker to my local and it's all fine now. Thanks! – Sefa Oct 17 '15 at 18:21
  • Great to see that, Enjoy mighty UI-Router ;) – Radim Köhler Oct 17 '15 at 18:27