4

I'm trying to build a navigational menu like any social networking site i.e. If i'm logged-Out i Can see the input fields asking for Username and Password but if I'm logged-In then i get to see my profile ,settings ,etc.

I'm trying to do the same thing but Cannot thing of an approach to this i Just need to know the way i can do this. I know the use case in which angular directive are used like ng-if ,etc but i'm thinking of using partials in some way.

I'm Using AngularJs with UI-Router.

user3459110
  • 6,961
  • 3
  • 26
  • 34
Ankit Ladhania
  • 1,005
  • 1
  • 12
  • 19
  • 1
    You should start with reading docs, observing the sample/example app and the you will see that this is a task for a multi-view feature. Here you can find some [links](http://stackoverflow.com/a/25499004/1679310) where to start – Radim Köhler Aug 28 '14 at 11:38
  • i know the use of `ng-*` directives but i want to use partials(if i can) – Ankit Ladhania Aug 28 '14 at 11:52
  • Have you seen my link? it is about `ui-router`, the really only reasonable tool for state/views managemetn... I **never** mentioned any `ng-*`... – Radim Köhler Aug 28 '14 at 11:54
  • @RadimKöhler i have Read the link that you suggested but still couldnt find the solution – Ankit Ladhania Aug 28 '14 at 19:12
  • @AnkitLadhania if I understood correctly you want to built a menu. Can you please point out what the problem is? – Dalorzo Aug 28 '14 at 20:39
  • @Dalorzo I want to make the template of the menu dynamic by using partials i.e. just like facebook or github if the user is not logged in i will show them the `Please login or Sign Up` on tab but if they are logged in i will show them different tabs related to user. I want to do it in SPA without reloading a page or refreshing. – Ankit Ladhania Aug 28 '14 at 20:47
  • @AnkitLadhania what is wrong with `ng-if`? – Dalorzo Aug 28 '14 at 20:50
  • @Dalorzo as per me it can be defined in a state. can it? and as i'm using ui-router i think it is better to do everything in same follow. What do you think? – Ankit Ladhania Aug 28 '14 at 20:53
  • 1
    @AnkitLadhania, glad, really glad that you've went through resources mentioned above. Really good. So, to speead up your ui-router usage, I've created an example for you. Hope it will help some how ;) Enjoy the amazing lib ui-router ;) – Radim Köhler Aug 29 '14 at 07:03
  • If that helped anyhow, awesome ;) – Radim Köhler Aug 29 '14 at 07:15

2 Answers2

3

The ui-router solution could be like in this plunker, I've created to show the ui-router way. There is no need to use some view rendering directives ala ng-if, ng-show... which in fact moves the logic from business into the view.

The example shows a comprehensive design, with redirections for unauthorized users, as well to grant the public accesss to anybody, supporting log on/off in one view, etc.

Better solution is to profit from built-in features of the ui-router, e.g.:

Templates (let me cite:)

TemplateUrl
... templateUrl can also be a function that returns a url. It takes one preset parameter, stateParams, which is NOT injected.

TemplateProvider
Or you can use a template provider function which can be injected, has access to locals, and must return template HTML...

So, what we can see in our example. Firstly there is a root state with two views, one of them is standard anchor, for all main child unnamed views ui-view=""

$stateProvider
.state('root', {
  abstract: true,
  views: {
    '': {templateUrl: 'tpl.root.html', },
    'loginfo@root': {
      templateProvider: templateSelector,
      controller: 'LoginCtrl',
    }
  }
})

where the templateSelector would be like this:

var templateSelector = function($http, UserService) 
{
  var templateName = UserService.isLogged
    ? "tpl.loggedin.html"
    : "tpl.loggedoff.html"
    ;

  return $http
    .get(templateName)
    .then(function(tpl){
      return tpl.data;
    });
};

Relying on simple setting in this example:

.factory('UserService', function() {
  return {
    isLogged: false,
  };
})

And that's it, we do have two templates:

  • "tpl.loggedin.html"
  • "tpl.loggedoff.html"

The remaining stuff is pretty expectable, state definitions, some redirection on unauthorized access... please observe the example.

which are interchanged based on the fact if user is logged on or off

Check the solution in this working plunker

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
0

You could use one of the following angular directives: ng-if, ng-show, ng-hide

Check in your controller if the user has signed in successfully and assign this value a ($scope) variable and use this as a condition to either show or hide certain elements of your navigation bar.

rmuller
  • 1,837
  • 12
  • 12