2

Hi guys I've created 2 directives, one for each object that I have, one is adminGroups, the other is adminProfiles. I have this problem, the 2nd directive is not called at all. This is the controller:

angular.module('Modulx').controller('AdminsController', ['$scope', 'AdminService'
    function ($scope, AdminService ) {

        $scope.hasLoad = false;
        $scope.groups= null;
        $scope.profiles = null;

        AdminService.getAdmins().then(function (response) {
            $scope.hasLoad = true;
            $scope.groups= response.data.Groups;
            $scope.profiles = response.data.Profiles;
        });
    }
]);

And i have this html section, where when data has been finished to load, i assume that both directives should be called

<section class="section admin-section" ng-if="hasLoad">
    <administrator-group data="groups" />

    <administrator-profile data="profiles " />
</section>

This is the directive structure they are the same, and the 2nd (which is not called) is just as this except that word "Group" has been replaced by "profile". This one works just fine.

angular.module('Modulex').directive('administratorGroup', [
    function () {
        return {
            restrict: 'E',
            scope: { data: '=groups' },
            templateUrl: '/Content/AngularApp/Admin/AdminGroups/Template.html',
            link: function (scope, element, attrs) { },
            controller: function ($scope) {
                $scope.data;
            }
        };
    }
]);

What is the problem? why is the 2nd directive ignored or not being called? Thank you.

The other directive

angular.module('Modulex').directive('administratorProfile', [
    function () {
        return {
            restrict: 'E',
            scope: { data: '=profiles' },
            templateUrl: '/Content/AngularApp/Admin/AdminProfiles/Template.html',
            link: function (scope, element, attrs) { },
            controller: function ($scope) {
                $scope.data;
            }
        };
    }
]);

If i exclude the group directive the profile directive will work just fine.

4
  • change administratorGroup to administratorProfile, or include other directive as well Commented Mar 13, 2017 at 8:51
  • post the other directive just in case Commented Mar 13, 2017 at 8:51
  • i've just added it Commented Mar 13, 2017 at 8:53
  • there is an extra [] in the directive Commented Mar 13, 2017 at 8:56

3 Answers 3

1

need to close your directive elements tags

change this

<administrator-group data="groups" />
<administrator-profile data="profiles " />

to this

<administrator-group data="groups"></administrator-group>
<administrator-profile data="profiles "></administrator-profile>

and change directive scope binding like this

this scope: { data: '=groups' } to this scope: { data: '=' }

Here's demo

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

1 Comment

yes. i've just figure it out but with another very similar approach. cheers mate
1

Try this way. The problem is with your directive implementation,

angular.module('Modulex').directive('administratorProfile',function(){
        return {
            restrict: 'E',
            scope: { data: '=profiles' },
            templateUrl: '/Content/AngularApp/Admin/AdminProfiles/Template.html',
            link: function (scope, element, attrs) { },
            controller: function ($scope) {
                $scope.data;
            }
        };
});


angular.module('Modulex').directive('administratorGroup',function(){
            return {
                restrict: 'E',
                scope: { data: '=groups' },
                templateUrl: '/Content/AngularApp/Admin/AdminGroups/Template.html',
                link: function (scope, element, attrs) { },
                controller: function ($scope) {
                    $scope.data;
                }
            };
    });

Comments

1

The fix is as follows. In the main html template, the controller template, i've moved the 2 directives in 2 separate html elements

<section class="section admin-group-section" ng-if="hasLoad">
    <administrator-group groups="groups" />
 </section>
<section class="section admin-profile-section" ng-if="hasLoad">
    <administrator-profile profiles="profiles" />
</section>

though i don't quite understand what the problem would be to have 2 directives in same html element.

Comments

Your Answer

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