1

I'm trying to migrate from Angular 1.4 to angular 1.5 components, then to typescript:

I want to use Angular 1.5 component, like that her is my code it's incomplet, but this the idea, behind

angular.
module('cool.core').
component('hasAnyRole', {
    template: 'Hello, {{$ctrl.user}}!',
    controller: function GreetUserController() {
        this.user = 'world';
    }
})

instead of Angular-directive-link:

angular.module('cool.core')
    .directive('hasAnyRole', ['Principal', function (Principal) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                var roles = attrs.hasAnyRole.replace(/\s+/g, '').split(',');

                var setVisible = function () {
                    element.removeClass('hidden');
                };
                var setHidden = function () {
                    element.addClass('hidden');
                };

                var defineVisibility = function (reset) {
                    var result;
                    if (reset) {
                        setVisible();
                    }

                    result = Principal.isInAnyRole(roles);
                    if (result) {
                        setVisible();
                    } else {
                        setHidden();
                    }
                };

                if (roles.length > 0) {
                    defineVisibility(true);
                }
            }
        };
    }])

However I don't find enough examples Thanks in advance for your suggestions.

Naou
  • 726
  • 1
  • 10
  • 23
  • 1
    And how do you want to use it as a component? Because right now you have an attribute directive, but component will be an element. Anyway, "has-any-role" doesn't feel like a component, so you probably want to leave it as a directive. – dfsq Aug 04 '16 at 15:28
  • I'm still confused, if I can really get out of directive in this case. So any suggestion to translate it to a component that will have the same role will be good. – Naou Aug 04 '16 at 15:42
  • You didn't explain the role of this directive, so it's not clear if it makes sense or not, so hard to advice something. – dfsq Aug 04 '16 at 15:43
  • it was used by jhipster, and i want to replace it – Naou Aug 04 '16 at 15:52
  • It should use `ng-class` and `hasAnyRole` binding. – Estus Flask Aug 04 '16 at 16:01
  • Can I have an example please? – Naou Aug 05 '16 at 07:56

0 Answers0