admin管理员组文章数量:1332881
I am using ui router states. I have two different controllers it may be extend also in future. How to write default and mon header & footer.
var myApp = angular.module('myApp', ['ui.router']);
myApp.controller('MainCtrl', function($scope) {});
myApp.config(function($stateProvider, $urlRouterProvider) {
// default route
$urlRouterProvider.otherwise("/first");
// ui router states
$stateProvider
.state('first', {
url: "/first",
views: {
header: {
template: '<h1>First header</h1>',
controller: function($scope) {}
},
content: {
template: '<p>First content</>',
controller: function($scope) {}
},
footer: {
template: '<div>First footer</div>',
controller: function($scope) {}
}
}
})
.state('second', {
url: "/second",
views: {
header: {
template: '<h1>Second header</h1>',
controller: function($scope) {}
},
content: {
template: '<p>Second content</>',
controller: function($scope) {}
},
footer: {
template: '<div>Second footer</div>',
controller: function($scope) {}
}
}
});
});
Jsfiddle : /
I am using ui router states. I have two different controllers it may be extend also in future. How to write default and mon header & footer.
var myApp = angular.module('myApp', ['ui.router']);
myApp.controller('MainCtrl', function($scope) {});
myApp.config(function($stateProvider, $urlRouterProvider) {
// default route
$urlRouterProvider.otherwise("/first");
// ui router states
$stateProvider
.state('first', {
url: "/first",
views: {
header: {
template: '<h1>First header</h1>',
controller: function($scope) {}
},
content: {
template: '<p>First content</>',
controller: function($scope) {}
},
footer: {
template: '<div>First footer</div>',
controller: function($scope) {}
}
}
})
.state('second', {
url: "/second",
views: {
header: {
template: '<h1>Second header</h1>',
controller: function($scope) {}
},
content: {
template: '<p>Second content</>',
controller: function($scope) {}
},
footer: {
template: '<div>Second footer</div>',
controller: function($scope) {}
}
}
});
});
Jsfiddle : http://jsfiddle/karthikreddy/b7cnszdf/
Share Improve this question asked Oct 7, 2015 at 10:50 KarthikKarthik 1,4883 gold badges17 silver badges29 bronze badges2 Answers
Reset to default 3Please see this demo : http://jsfiddle/tkf954a5/
You can define your footer and header like :
var header = {
template: '<h1>Im Header</h1>',
controller: function($scope) {}
}
and after that use it in your states :
.state('first', {
url: "/first",
views: {
header: header,
content: {
template: '<p>First content</>',
controller: function($scope) {}
},
footer: footer
}
})
I'd advise you to put your view (html) and controller code in separate files to make things more readable.
Ui-router allows you to create nested states. With that you can create a base state which has your header and footer, and than add all yout states as nested states.
See: https://github./angular-ui/ui-router/wiki/Nested-States-and-Nested-Views for more info.
It would look something like this:
.state('base',{
url: '',
views: {
'header': {
controller: 'HeaderCtrl',
templateUrl: 'views/header.html'
},
'main': {
template: '<ui-view/>'
},
'footer': {
controller: 'FooterCtrl',
templateUrl: 'views/footer.html'
}
}
})
.state('base.first', {
url: '/first',
controller: 'FirstCtrl',
templateUrl: 'first.html'
})
.state('base.first', {
url: '/second',
controller: 'SecondCtrl',
templateUrl: 'second.html'
})
EDIT
If you don't want to use nested states, you could just put your views and controllers in separate files and include them on each route you make. Instead of having the whole code there, it would be just one string.
本文标签: javascriptHow to write common header amp footer in Angularjs using ui router statesStack Overflow
版权声明:本文标题:javascript - How to write common header & footer in Angularjs using ui router states - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742310113a2450696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论