1

Hi I am getting problem while implementing 404 page using angular ui router' Scenario when the user type something in the url which was not present in the app i have to show 404 page not found page

What I tried

app.js

var myapp = angular.module('myapp', ['ui.router']);

myapp.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/xxx/xxxoverview');

    $stateProvider.state('xxx', {
        url:'/xxx',
        templateUrl :'partials/xxx/xxxOverview.html',
        controller:'xxxAccordionCtrl'
    })
     .state('404', {
        url:'/404',
        templateUrl :'404.html'

    });

My Actual Design

enter image description here

I am getting the following response enter image description here

I am expecting

enter image description here

0

2 Answers 2

2

In order to get what you are expecting you need to write your layout better.

You layout is currently:

 <div>
     .. some layout code here 
     <div> navigation here </div>
     <div ui-view></div>
     ..some more layout
 </div>

in order to get the result you want you have use nested ui-views

 <div>
       .. outer layout
       <div ui-view> 
             .. inner layout
             <div> navigation here </div>
             <div ui-view></div>
             .. inner layout cont. 
       </div>
        .. outer layout cont. 
</div>

and change the routing accordingly.

if you give me a couple of minutes I will prepare a plunkr for you.

So this is a plunker for you: http://plnkr.co/edit/Y6PqRhrbvpEpWApm9lkS?p=preview

and this is the breakdown of the code I did

$stateProvider
     .state('layout', {
       template: 'my navigation <div ui-view></div>'

     } )
     .state('layout.page', {
       url: '/page',
       template: 'this is my content go see a missing page <a href="#/404">missing page link</a>'
     })
     .state('404', {
       url:'/404',
       template: '404!!! page is missing'
     });

As you can see I have an "abstract" state called "layout", which basically defines template but that's it! no url, no controller no nothing.

I add the navigation in the template, and then another ui-view. - NOTE I already have a ui-view on my index.html. other than that that index.html can be empty as far as I care. I added hello there just so you have a point of reference.

then my page is a nested state inside layout. see state layout.page. This is what is so great in angular-ui-router. This way you can have different layouts for different sections of the website. So my page uses the layout simply by nesting the state.

then I have a different state which I called 404. Since it does not inherit from layout, it will not have the navigation.

Sign up to request clarification or add additional context in comments.

5 Comments

@RavindharKonka - added plunker + explanation. if you have any question please let me know.
I have gone through the plunker But how can I valid the url .In the plunker u gave link to navigate 404 page But if the user enter invalid user then i want 404 page can you please help me
@RavindharKonka - that is a different problem and you can find the solution at:: stackoverflow.com/questions/23438664/…
thanks @guy mograbi you are resolve my problem and I supported (UpVote) this answer :)
@RavindharKonka please consider marking my answer as the solution (press the check mark) so it will not appear as "open" in the search.
1

Have you your index.html like this?

<nav>You navigation</nav>
<ui-view> </ui-view>

You can put your <nav>You navigation</nav> in templateUrl :'partials/xxx/xxxOverview.html'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.