1

I've set up two simple URLs, with simple different templates and the same controller, but it doesn't work

HEAD:

<script src="jsLib/angular_v1.4.js" type="text/javascript"></script>
<script src="jsLib/angular-ui-router.min.js" type="text/javascript"></script>
<script src="routes.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>

HTML:

<body ng-controller="MainCTRL as ctrl">
{{ctrl.nameApp}}
<div ui-view></div>

app.js:

angular
    .module("app", ["ui.router"])
    .controller("MainCTRL", MainCTRL)
    .config(configA);


function MainCTRL($location){
    this.nameApp = "nameApp";
}

routes.js:

function configA($urlRouterProvider, $stateProvider){

    $urlRouterProvider.otherwise("/");

    $stateProvider
        .state("/",{
           url: "/",
           templateUrl : "/testingBlock.htm",
           controller : "MainCTRL"
        })
        .state("login",{
            url: "/login",
            templateUrl : "/app/templates/login.htm",
            controller : "MainCTRL"
        });

}

login.htm:

<div>Header</div>

<div>Main</div>

<div>Footer</div>

{{name.nameApp}}

testingBlock.htm:

 <h2>Hello goHenry</h2>

{{MainCTRL.nameApp}}

It doesn't display MainCTRL.nameApp

5
  • Your console probably display an error, could you copy it ? Commented Jun 2, 2015 at 11:00
  • try updating angular version to 1.3.16 Commented Jun 2, 2015 at 11:06
  • The my one is 1.4 version Commented Jun 2, 2015 at 11:09
  • you are creating new instance of controller inside view and not using controllerAs so MainCTRL is undefined in expressions Commented Jun 2, 2015 at 11:10
  • @Donovant seems it helped ;) great, enjoy mighty UI-Router, sir ;) Commented Jun 2, 2015 at 11:19

1 Answer 1

1

Wanted to show you where is the issue, but did not found any. But then I realized that the issue is inside of the state views.

There is a working version of your code.

All adjustments are really very small (not important). The index.html

<html ng-app="app" >

  <head>
    <title>my app</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js"
    ></script>
    <script src="//rawgit.com/angular-ui/ui-router/0.2.15/release/angular-ui-router.js"
    ></script>
    <script src="routes.js"></script>
    <script src="app.js"></script>
  </head> 

  <body ng-controller="MainCTRL as ctrl">
    {{ctrl.nameApp}}

    <br><a href="#/">#/</a> - default 
    <br><a href="#/login">login</a>

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

  </body> 
</html>

The big one is here "controllerAs"

$stateProvider
    .state("/",{
       url: "/", 
       templateUrl : "testingBlock.htm",
       controller : "MainCTRL",
       controllerAs:"MainCTRL",
    })
    .state("login",{
        url: "/login",
        templateUrl : "app/templates/login.htm",
        controller : "MainCTRL",
        controllerAs:"name",
    });

So, what is different? to support these statements:

  • {{name.nameApp}} in the "testingBlock.htm",
  • {{MainCTRL.nameApp}} in the "app/templates/login.htm",

We need to use controllerAs and name as name or MainCTRL (compare your code and this example)

NOTE: do not mix ng-controller and UI-Router states controllers. These should be different objects, because UI-Router can use other resolve then the angular native ng-controller. Later it could bring surprises...

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

5 Comments

but it's not printing the OP expression inside the view
@charlietfl thanks for help, finally we should have working plunker and the answer ;)
Yep, I've just realized 30sec ago that I needed to set "controller" and "controllerAs"
controllerAs is the whole answer really, the not important part of this answer is really incorrect...it is important
You can just set up "controllerAs", that's it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.