问题
I have a controller in a folder named controller at the root. I have view that I want to attach to the corresponding views when the route changes. I am using angular-ui-router.
controllers/one.js
angular.module('sub',["ui.router"]).config(function($stateProvider){
$stateProvider
.state('index', {
url: "/login",
views: {
"viewA": {
templateUrl: "login.html"
}
}
})
.state('register', {
url: "/register",
views: {
"viewA": {
templateUrl: "register.html"
}
}
})
.state('upload', {
url: "/upload",
views: {
"viewA": {
templateUrl: "upload.html"
}
}
})
})
test.html
<!DOCTYPE html>
<html ng-app="sub">
<head>
<title>AngularJS: UI-Router Quick Start</title>
<!-- Bootstrap CSS -->
<link href="bootstrap.min.css" rel="stylesheet">
</head>
<body class="container">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" ui-sref="index">Quick Start</a>
<ul class="nav">
<li><a ui-sref="index">Home</a></li>
<li><a ui-sref="register">Register</a></li>
<li><a ui-sref="upload">upload</a></li>
</ul>
</div>
</div>
<div class="row">
<div class="span6">
<div class="well" ui-view="viewA"></div>
</div>
</div>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="Controllers/one.js"></script>
<script type="text/javascript" src="angular-cookies.js"></script>
<script type="text/javascript" src="Controllers/loginController.js"></script>
<script type="text/javascript" src="Controllers/uploadController.js"></script>
<script type="text/javascript" src="Controllers/registerController.js"></script>
<script type="text/javascript" src="angular-ui-router.js"></script>
<script type="text/javascript" src="jquery-2.1.3.min.js"></script>
</body>
</html>
login.html
<div ng-controller="loginController">
<p>
UserName:
<input id="director" type="text" ng-model="mUserName"/>
</p>
<p>
Password:
<input id="actor" type="text" ng-model="mPassWord"/>
</p>
<button ng-click="login()">Login</button>
</div>
Controllers/LoginController.js
angular.module('subsd',["ngCookies"]).controller('loginController', ['$http','$scope','$cookies',function($http,$scope,$cookies) {})]);
If I mention 'sub' instead of 'subsd' in loginController.js the routing stops working.
回答1:
The final answer includes few things to do:
- Remove
<div ng-controller="loginController">
from the login.html template file. - Move the
'ngCookies'
dependency to thesub
module definition in controllers/one.js file (angular.module('sub',["ui.router", "ngCookies"])
). - In the controllers/logincontroller.js file change the module name from
subsd
tosub
back again and remove the second argument from thesub
module invoke, to make it look like this:angular.module('sub').controller(...)
.
When you changed the module name before, you were trying to re-define it, which is an error.
This
angular.module('moduleName', [...dependencies])
is the module setter, while this
angular.module('moduleName')
is the module getter.
回答2:
Do you mean:
$stateProvider
.state('index', {
url: "/login",
views: {
"viewA": {
templateUrl: "login.html",
// THIS ?
controller: "LoginController"
}
}
})
})
Also remember to remove the ng-controller
attribute from the view.
来源:https://stackoverflow.com/questions/28727173/how-to-bind-controllers-contained-in-a-folder-to-views-in-angularjs-dynamically