How to reload an Angularjs Directive in the Controller?

Asked

Viewed 104 times

0

Personal greetings, I am developing an app with login and I am using Angularjs, but I have a cruel doubt when logging in the user.

It turns out that I am using the user menu via Directive, when the user is not logged in to the system it shows the default links (Sign in, Register, etc...), but even after the user logs into the system it remains the same menu. I would like when the user logs in he reloads the Directive from the menu to show the user options.

My Directive is the following:

app.directive("menuAluno", function() {
  var linker = function(scope, element, attrs) {
    // DO SOMETHING
  }

  return {
    templateUrl: "menu",
    scope: { menuAluno: '=menuAluno' },
    controller: "menuCtrl"
  };
});

1 answer

0


Friend your Directive is very vague, in your controller create a user object and fill it with the data of your user coming from where you are pulling, and leave the Directive only for the same menu, example:

app.directive("menuAluno", function() {
  return {
    templateUrl: scope.urlMenu,
    scope: {
       menuAluno: '=menuAluno',
       objAluno: '='
    },
    link: function(scope){
           scope.urlMenu = (scope.ubjAluno ? 'menu-aluno.html' : 'menu-defalt.html');
    },
    controller: "menuCtrl"
  };
});

in the example given above I receive an object objAluno and then on the link check if the user is filled in(logged in) scope.urlMenu = (scope.ubjAluno ? 'menu-aluno.html' : 'menu-defalt.html'); if yes I pass a Url to the templateUrl, If I do not pass another url to the template, ai la in your page you pass the user object as parameter to the example component <menu-aluno obj-aluno="vm.usuario"></menu-aluno>

  • Thanks friend for the answer, there is some way Directive is "watching" the object change so it update the menu without having to refresh the page when the user logs in?

  • has yes, you can put a $watch() inside the directive by listening to the objAluno and when it is changed it executes the <code>Scope.urlMenu = (Scope.ubjAluno ? 'menu-student.html' 'menu-defalt.html');</code> so they would be checking the user value always, from a glance at the $watch $apply and $Digest to better understand the date bind of the Angularjs, here is an example of $Whatch() http://jsfiddle.net/user2314737/jmf6kL3n/ in this example he is listening to an ng-model="myName" and saying if it is larger or more than 5 characters

  • Thank you again for the help, I will take a test... I am very grateful. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.