admin管理员组文章数量:1022623
i am having a problem and a question
my cliend side uses:
<div ng-view></div>
and the following scripts:
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-resource.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>
<script src="js/services.js"></script>
<script src="js/filters.js"></script>
my routProvider was:
angular.module('myApp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
$routeProvider.otherwise({redirectTo: '/view1'});
}]);
which worked fine.
i can't figure out why this implemention doesn't work, i saw it used many times before:
var myApp = angular.module('myApp', []);
myApp.config(function($routeProvider) {
$routeProvider.
when('/view1', {
controller: 'MyCtrl1',
templateUrl: 'partials/partial1.html'
}).
when('/view2', {
controller: 'MyCtrl2',
templateUrl: 'partials/partial2.html'
}).
otherwise( {redirecTo: '/view1'});
});
another question is: why in the first example there is '$routeProvider' injection before the function? as i understand the function($routProvider) should do this job.
thanks.
i am having a problem and a question
my cliend side uses:
<div ng-view></div>
and the following scripts:
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-resource.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>
<script src="js/services.js"></script>
<script src="js/filters.js"></script>
my routProvider was:
angular.module('myApp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
$routeProvider.otherwise({redirectTo: '/view1'});
}]);
which worked fine.
i can't figure out why this implemention doesn't work, i saw it used many times before:
var myApp = angular.module('myApp', []);
myApp.config(function($routeProvider) {
$routeProvider.
when('/view1', {
controller: 'MyCtrl1',
templateUrl: 'partials/partial1.html'
}).
when('/view2', {
controller: 'MyCtrl2',
templateUrl: 'partials/partial2.html'
}).
otherwise( {redirecTo: '/view1'});
});
another question is: why in the first example there is '$routeProvider' injection before the function? as i understand the function($routProvider) should do this job.
thanks.
Share Improve this question edited Aug 2, 2013 at 11:31 Henrik Andersson 47.3k16 gold badges100 silver badges94 bronze badges asked Aug 2, 2013 at 11:12 lobengula3rdlobengula3rd 1,8615 gold badges27 silver badges39 bronze badges 03 Answers
Reset to default 5Your code works just fine (plnkr), but you misspelled redirectTo
so the initial redirect to /view1
didn't work.
With regard to explicitly injecting $routeProvider
: using the explicit version of injection prevents issues with variable renaming when you minify your JS code for production use. For instance, without being explicit, your code might be minified into something like this:
a.config(function(b) { b.when('/view1', ...) });
Since Angular depends the name of the argument to inject the correct provider, you can explicitly name it as a string (which won't get minified):
a.config([ '$routeProvider', function(b) { ... } ]);
That way, Angular knows the first argument should be $routeProvider
.
You are super close. You do need to include the $routeprovider. This should work for you.
angular.module('myApp', [])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
when('/view1', {
controller: 'MyCtrl1',
templateUrl: 'partials/partial1.html'
}).
when('/view2', {
controller: 'MyCtrl2',
templateUrl: 'partials/partial2.html'
}).
otherwise({
redirectTo: '/view1'
});
}]);
This should work!
angular.module('myApp', [])
.config(function($routeProvider) {
$routeProvider.when('/view1', {
controller: 'MyCtrl1',
templateUrl: 'partials/partial1.html'
});
$routeProvider.when('/view2', {
controller: 'MyCtrl2',
templateUrl: 'partials/partial2.html'
});
$routeProvider.otherwise({
redirectTo: '/view1'
});
});
i am having a problem and a question
my cliend side uses:
<div ng-view></div>
and the following scripts:
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-resource.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>
<script src="js/services.js"></script>
<script src="js/filters.js"></script>
my routProvider was:
angular.module('myApp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
$routeProvider.otherwise({redirectTo: '/view1'});
}]);
which worked fine.
i can't figure out why this implemention doesn't work, i saw it used many times before:
var myApp = angular.module('myApp', []);
myApp.config(function($routeProvider) {
$routeProvider.
when('/view1', {
controller: 'MyCtrl1',
templateUrl: 'partials/partial1.html'
}).
when('/view2', {
controller: 'MyCtrl2',
templateUrl: 'partials/partial2.html'
}).
otherwise( {redirecTo: '/view1'});
});
another question is: why in the first example there is '$routeProvider' injection before the function? as i understand the function($routProvider) should do this job.
thanks.
i am having a problem and a question
my cliend side uses:
<div ng-view></div>
and the following scripts:
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-resource.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/directives.js"></script>
<script src="js/services.js"></script>
<script src="js/filters.js"></script>
my routProvider was:
angular.module('myApp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
$routeProvider.otherwise({redirectTo: '/view1'});
}]);
which worked fine.
i can't figure out why this implemention doesn't work, i saw it used many times before:
var myApp = angular.module('myApp', []);
myApp.config(function($routeProvider) {
$routeProvider.
when('/view1', {
controller: 'MyCtrl1',
templateUrl: 'partials/partial1.html'
}).
when('/view2', {
controller: 'MyCtrl2',
templateUrl: 'partials/partial2.html'
}).
otherwise( {redirecTo: '/view1'});
});
another question is: why in the first example there is '$routeProvider' injection before the function? as i understand the function($routProvider) should do this job.
thanks.
Share Improve this question edited Aug 2, 2013 at 11:31 Henrik Andersson 47.3k16 gold badges100 silver badges94 bronze badges asked Aug 2, 2013 at 11:12 lobengula3rdlobengula3rd 1,8615 gold badges27 silver badges39 bronze badges 03 Answers
Reset to default 5Your code works just fine (plnkr), but you misspelled redirectTo
so the initial redirect to /view1
didn't work.
With regard to explicitly injecting $routeProvider
: using the explicit version of injection prevents issues with variable renaming when you minify your JS code for production use. For instance, without being explicit, your code might be minified into something like this:
a.config(function(b) { b.when('/view1', ...) });
Since Angular depends the name of the argument to inject the correct provider, you can explicitly name it as a string (which won't get minified):
a.config([ '$routeProvider', function(b) { ... } ]);
That way, Angular knows the first argument should be $routeProvider
.
You are super close. You do need to include the $routeprovider. This should work for you.
angular.module('myApp', [])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
when('/view1', {
controller: 'MyCtrl1',
templateUrl: 'partials/partial1.html'
}).
when('/view2', {
controller: 'MyCtrl2',
templateUrl: 'partials/partial2.html'
}).
otherwise({
redirectTo: '/view1'
});
}]);
This should work!
angular.module('myApp', [])
.config(function($routeProvider) {
$routeProvider.when('/view1', {
controller: 'MyCtrl1',
templateUrl: 'partials/partial1.html'
});
$routeProvider.when('/view2', {
controller: 'MyCtrl2',
templateUrl: 'partials/partial2.html'
});
$routeProvider.otherwise({
redirectTo: '/view1'
});
});
本文标签: javascriptrouteProvider will not work as should beStack Overflow
版权声明:本文标题:javascript - $routeProvider will not work as should be - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745525579a2154509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论