Reputation: 3161
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
Upvotes: 1
Views: 49
Reputation: 123861
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...
Upvotes: 1