I have the following code for my router:
.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
$stateProvider
.state('main', {
url: '/',
templateUrl: 'templates/main.html',
controller: 'MainCtrl'
})
.state('login', {
url: '/login',
templateUrl: 'templates/login.html',
})
.state('signup', {
url: '/signup',
templateUrl: 'templates/signup.html',
controller: 'SignupCtrl'
})
.state('userEdit', {
url: '/user/edit',
templateUrl: 'templates/user/edit.html',
controller: 'UserEditCtrl'
})
.state('workouts', {
url: '/workouts',
templateUrl: 'templates/workouts/index.html',
controller: 'WorkoutsCtrl'
})
.state('workouts.show', {
url: '/:id',
templateUrl: 'show.html',
controller: 'WorkoutCtrl',
onEnter: function() {
console.log('this is stupid');
}
})
$urlRouterProvider.otherwise('/');
}])
My issue is with the workouts.show state. I have this link:
<div class="" ng-repeat="workout in workouts track by workout._id">
<hr>
<a ui-sref="workouts.show({id: workout._id})">{{ workout.shortURI }}</a>
<p>{{ workout.description }}</p>
<ul class="exercise-list">
<li ng-repeat="exercise in workout.exercises">{{ exercise }}</li>
</ul>
<hr>
</div>
When I click on the link with ui-sref"workouts.show({id: workout._id})" the url changes to the proper format, and adds the id dynamically, but the view does not load. It just stays on the same page.
I have seen several other questions that are similar, but none of the accepted solutions have worked for me, and I have spent too long on this now.
Thanks in advance for any help!
I had similar issue.
You must have a ui-view in your "workouts" state, so that your "workouts.show" will render as a child.
Hope this helps.
Related
I'm using ui-router for state handling. This works fine, but now I have to create page 404 and would like to display it on the whole page and not inside the page as other pages.
app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider',
function ($stateProvider, $urlRouterProvider, $locationProvider) {
$locationProvider.hashPrefix('!').html5Mode({
enabled: true,
requireBase: false
});
$stateProvider
.state('stateIndex', {
url: '/',
templateUrl: '/templates/list.html',
controller: 'dashListController'
})
.state('stateList', {
url: '/list',
templateUrl: '/templates/list.html',
controller: 'dashListController'
}).state('stateDashboard', {
url: '/dashboard/:id',
templateUrl: '/templates/dashboard.html',
controller: 'dashboardController'
})
.state('stateWidgetsList', {
url: '/widgetsList',
templateUrl: '/templates/widgetsList.html',
controller: 'widgetsListController'
})
.state('404', {
url: '/404',
templateUrl: '/templates/404.html'
});
}]);
and on my index.html I have
<div ui-view></div>
where I display all the pages, outside of this I have logo, menu, etc.. which I would like to hide while displaying 404 page.
How can I do it?
Personally I would redesign the index.html, and bring the outer template (logo, menu, etc), into it's own template and state. Then you can sit child states below it in the ui-router hierarchy. For example:
$stateProvider
.state('app', {
abstract: true,
url: '',
templateUrl: '/templates/appcontainer.html'
})
.state('app.stateIndex', {
url: '/',
templateUrl: '/templates/list.html',
controller: 'dashListController'
})
.state('404', {
url: '/404',
templateUrl: '/templates/404.html'
});
Then you just need to put your logos/menus, etc inside appcontainer.html, and then just have a single <div ui-view></div> inside your index.html. Also if you do it this way, don't forget to add the child ui-view inside appcontainer.html.
You can create a root parent state that will contain your layout stuff (logo, menu, etc) and have the 404 live outside of that.
routes
$stateProvider
.state('root', {
abstract: true, // makes this state not directly accessible
templateUrl: 'root.html'
})
.state('root.stateIndex', {
url: '/',
templateUrl: '/templates/list.html',
controller: 'dashListController'
})
// ...
.state('404', {
url: '/404',
templateUrl: '/templates/404.html'
});
root.html
<nav><!-- menu stuff --></nav>
<ui-view></ui-view>
<footer></footer>
I am a beginner on Ionic/Angularjs.
Developing a side menu app on windows 10.
I populated a page from a select statement on SQLite.
And i'm to edit individual item by navigating to an edit page using state params but not responding. Many thanks for your kind assistance:
<a href="#/edit/{{task.id}}" class="item" ng-repeat="task in Ourdata">
<h2>{{task.id}}</h2>
<p>{{task.name}}</p>
</a>
.config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state('list',{
url: '/list',
templateUrl: 'templates/list.html'
})
.state('edit',{
url: '/edit:taskId',
templateUrl: 'templates/edit.html'
});
$urlRouterProvider.otherwise('/list');
})
You should insert the slash / before the :
I edited and add a controller to it:
.config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state('list',{
url: '/list',
templateUrl: 'templates/list.html'
})
.state('edit',{
url: '/edit/:taskId',
templateUrl: 'templates/edit.html',
controller: 'MyCtrl'
});
$urlRouterProvider.otherwise('/list');
})
In your controller:
.controller('MyCtrl', function($stateParams){
var taskId = $stateParams.taskId;
});
HTML
<a ui-sref='tango::new'>Create Tango</a>
tango.routes.js
angular
.module('tango')
.config(config)
;
function config($stateProvider) {
$stateProvider
.state('tango', {
url: '/tango/:id',
templateUrl: '/states/tangos/tango.html',
controller: 'TangoController as vm'
})
.state('tango::new', {
url: '/tango/new',
templateUrl: '/states/tangos/tango.html',
controller: 'TangoController as vm'
})
;
}
This is the request that is going out when I click the link:
GET http://localhost:3000/tangos/new
Why is this? How can I stop it from happening?
There is a working plunker
The point here (despite of a bit unusual state name tango::new - which is still absolutely valid) is: the order of state definition:
the first (sooner) must be defined more specific, the second (later) the more generic
So, this should be the proper order:
.state('tango::new', {
url: '/tango/new',
templateUrl: 'states/tangos/tango.html',
controller: 'TangoController as vm'
})
.state('tango', {
url: '/tango/:id',
templateUrl: 'states/tangos/tango.html',
controller: 'TangoController as vm'
})
With this order in place, these links will work:
//ui-sref
<a ui-sref='tango({id:1})'>tango({id:1})
<a ui-sref='tango({id:22})'>tango({id:22})
<a ui-sref='tango::new'>Create Tango
//href
<a href="#/tango/333">
<a href="#/tango/4444">
<a href="#/tango/new">
Check it here
I tryed simple layout but it seems nothing gets render. Layout.html is being loaded and that is all. There are no errors in console.
This is part of my config file
$stateProvider
.state('layout', {
url: '',
abstract: true,
templateUrl: viewsRoot + "layout.html",
controller: 'LayoutController',
})
.state('layout.sidebar', {
abstract: true,
templateUrl: viewsRoot + "sidebar.html",
controller: 'SidebarController'
})
.state('layout.mainview', {
abstract: true,
templateUrl: viewsRoot + "container.html",
})
.state('layout.mainview.object-details', {
url: '/object-details',
templateUrl: viewsRoot + "object-details.html",
controller: 'ObjectDetailsController'
})
.state('layout.mainview.home', {
url: '/',
templateUrl: viewsRoot + "home.html",
controller: 'HomeController'
})
$urlRouterProvider.otherwise('/');
This is layout.html:
<div ui-view="sidebar">
</div>
<div class="page-content-wrapper">
<div class="page-content" style="min-height: 1227px">
<!-- END PAGE SPINNER -->
<div ui-view="mainview">
</div>
<!-- END PAGE CONTENT-->
</div>
</div>
This is container.html
<ui-view></ui-view>
Should I use nested views instead of nested states?
You need to identify what renders where. You have two areas, "siderbar" and "mainview", but you are not identifying what template goes to what view in your state configs
$stateProvider
.state('layout.home',{
views: {
'mainview': {
templateUrl: '...',
controller: ...
},
'siderbar': {
templateUrl: '...',
controller: ...
}
}
})
See https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views for more information
How can I load a default child state while placing links to the child states in ui-sref and placing a an empty ui-view below. I want ui-view to load first child state by default on page load, and by user clicking ui-sref links, he/she switch between the states.
Following is the complete code explaining my scenario:
<div class="apps_head settings_top_head1">
Favorities
<img src="img/apps_menu.png"/>
<div id="row1Dropdown" style="border: solid; display: none">
<ul>
<li><a onclick="setRow1Toggle()" ui-sref="dashboard.mainContent.favorites" href="javascript:;" style="color: black">Favorities</a></li>
<li><a onclick="setRow1Toggle()" ui-sref="dashboard.mainContent.passwords" href="javascript:;" style="color: black">Passwords</a></li>
</ul>
</div>
<div ui-view></div>
//===================================================================================
//Here goes the UI-Router Configuration:
var myApp = angular.module('myApp', [
//'ngRoute',
'ui.router',
'JungleLockControllers'
]).run(function($rootScope, $state){
$rootScope.$state = $state;
});
myApp.config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state('home',{
url:'/home',
templateUrl:'partials/home.html',
controller : 'HomepageController'
})
.state('career',{
url:'/career',
templateUrl:'partials/career.html'
})
.state('faq',{
url:'/faq',
templateUrl:'partials/faq.html'
})
.state('companyOverview',{
url:'/companyOverview',
templateUrl:'partials/companyOverview.html'
})
.state('press',{
url:'/press',
templateUrl:'partials/press.html'
})
.state('terms',{
url:'/terms',
templateUrl:'partials/terms.html'
})
.state('dashboard',{
url:'/dashboard',
views:{
'':{templateUrl:'partials/dashboard.html'},
'notifications#dashboard':{templateUrl:'partials/dashboard.notifications.html'}
}
})
.state('dashboard.mainContent',{
url:'/mainContent',
//templateUrl:'partials/dashboard.mainContent.html'
views: {
'':{templateUrl:'partials/dashboard.mainContent.html'},
'contentHeader#dashboard.mainContent':{templateUrl:'partials/dashboardContentHeader.html'},
'row1#dashboard.mainContent':{templateUrl:'partials/row1.dashboard.html'},
'row2#dashboard.mainContent':{templateUrl:'partials/row2.dashboard.html'},
'row3#dashboard.mainContent':{templateUrl:'partials/row3.dashboard.html'},
//'favorites#dashboard.mainContent':{templateUrl: "partials/favouriteContent.html"},
//'passwords#dashboard.mainContent':{templateUrl: "partials/passwordsContent.html"},
'sponsored#dashboard.mainContent':{templateUrl: "partials/sponsoredContent.html"},
'browse#dashboard.mainContent':{templateUrl: "partials/browseContent.html"},
}
})
.state('dashboard.mainContent.favorites',{
url:'/favorites',
templateUrl:'partials/favouriteContent.html'
})
.state('dashboard.mainContent.passwords',{
url:'/passwords',
templateUrl:'partials/passwordsContent.html'
})
.state('dashboard.settings',{
url:'/settings',
templateUrl:'partials/dashboard.settings.html'
})
// .state('settings',{
// url:'/settings',
// templateUrl:'partials/dashboard.settings.html'
// })
.state('logout',{
url:'/logout',
templateUrl:'partials/logout.html'
})
/*
Template for adding new empty state
.state('',{
url:'',
templateUrl:''
})
*/
;
$urlRouterProvider.otherwise('/home');
});
Change your rounter config method as:
routerApp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home/list');
$stateProvider
// HOME STATES AND NESTED VIEWS ========================================
.state('home', {
url: '/home',
abstract: true,
templateUrl: 'partial-home.html'
})
// nested list with custom controller
.state('home.list', {
url: '/list',
templateUrl: 'partial-home-list.html',
controller: function($scope) {
$scope.dogs = ['Bernese', 'Husky', 'Goldendoodle'];
}
})
// nested list with just some random string data
.state('home.paragraph', {
url: '/paragraph',
template: 'I could sure use a drink right now.'
})
// ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================
.state('about', {
url: '/about',
views: {
'': { templateUrl: 'partial-about.html' },
'columnOne#about': { template: 'Look I am a column!' },
'columnTwo#about': {
templateUrl: 'table-data.html',
controller: 'scotchController'
}
}
});
});
Change your default URL
Make your home url state as abstract.
Hope this helps
I've found and implemented the solutions, so what i did is, I made the parent state as Abstract, and the child state that i wanted to be displayed by default, i gave a blank URL to it. here goes an example.
myApp.config(function($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise('/home');
$stateProvider.
.state('dashboard',{
abstract:true,
url:'/dashboard',
views:{
'':{templateUrl:'partials/dashboard.html',
controller:'DashboardController'},
'notifications#dashboard':{templateUrl:'partials/dashboard.notifications.html'}
}
})
.state('dashboard.mainContent',{
url:'',
views: {
'':{templateUrl:'partials/dashboard.mainContent.html',
controller:'DashboardController'},
'contentHeader#dashboard.mainContent':{templateUrl:'partials/dashboardContentHeader.html'},
//'contentHeader#dashboard.mainContent':{templateUrl:'partials/logoutConfirmationPrompt.html'},
'row1#dashboard.mainContent':{templateUrl:'partials/row1.dashboard.html'},
'row2#dashboard.mainContent':{templateUrl:'partials/row2.dashboard.html'},
'row3#dashboard.mainContent':{templateUrl:'partials/row3.dashboard.html'},
//'favorites#dashboard.mainContent':{templateUrl: "partials/favouriteContent.html"},
//'passwords#dashboard.mainContent':{templateUrl: "partials/passwordsContent.html"},
'sponsored#dashboard.mainContent':{templateUrl: "partials/sponsoredContent.html"},
'browse#dashboard.mainContent':{templateUrl: "partials/browseContent.html"},
}
})
This way my problem got solved, Thanks alot for your help.