-2

i created a plug-in for field

angular.module('ersProfileForm').directive('ersProfileEditableField', ['$templateCache', '$compile', 'profileFieldService', 'RolesService',
                                                           function($templateCache,   $compile,   profileFieldService , RolesService){
return {
    restrict: 'AE',
    templateUrl: '',
    scope: {
        ersProfileEditableField: '=',
        ersProfileSectionData: '=',
        ersProfileEditableFieldValue: '=',
        ersBulkEdit: '<'
    },
    controller: ['$scope', '$http','$q','$resource', function($scope, $http, $q, $resource){
        $http.get('rest/roles',{}).then(function(response){
            $scope.roles = response.data;
        }); 

    }],
    link: function(scope, iElement, iAttrs, controller){
        iElement.append(jQuery(profileFieldService.getTemplate(scope.ersProfileEditableField.type, scope)));
        $compile(iElement.contents())(scope);
    }
};
}]);

roles data will be used in this template

 angular.module('ersProfileForm').factory('profileFieldService', ['$resource', function($resource){
var factory = {};
factory.getTemplate = function(type, scope){
    scope.field = scope.ersProfileEditableField; 
    var tpl = '<div ng-repeat ="role in roles"'> 
            +' <label>{{role.name</label>'
            +' </div>'
        break;
return tpl;
};
return factory;
}]);

i want roles array in this template but service is taking time so roles is not defined in template it is executing after some time

my question is that i want roles data from the request only then go to template which is defined in link?

4
  • you need to call the template once data received, so move the function call getTemplate right after your $scope.roles at the moment the link bit gets called before the data is returned from your call. Commented Jan 7, 2017 at 9:53
  • i am not understanding it getTemplate can not be defined in controller after the $scope.roles it should be defined in link Commented Jan 7, 2017 at 10:05
  • Last comment makes no sense. Also makes no sense to use jQuery for this. Why do you even need a factory for the template? Commented Jan 7, 2017 at 12:55
  • it is big plug-in i have created it in short form i dont know why gave me negative vote Commented Jan 9, 2017 at 4:45

3 Answers 3

0

Try var tpl = '<div ng-repeat ="role in roles" ng-show="roles&&(roles.length>0)"> '+' <label>role.name</label>' +' </div>'

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

1 Comment

get data from the request only then go to template which is defined in link
0

Did you tried using the $q promise object ? resolve it in your $http method, then use it with .then :) Here is a link from the docs : https://docs.angularjs.org/api/ng/service/$q

1 Comment

ya i have used it own way provide some code whatever you want to say?
0

The template needs double curly brackets {{ }} to bind data to the DOM:

factory.getTemplate = function(type, scope){
    scope.field = scope.ersProfileEditableField; 
    var tpl = '<div ng-repeat ="role in roles"'> 
          // +' <label>role.name</label>'
          // USE {{ }} for interpolation
            +' <label>{{role.name}}</label>'
            +' </div>'
    return tpl;
};

The compile can be delayed by using a watcher:

link: function(scope, iElement, iAttrs, controller){
    scope.$watch("::roles", function (newValue) {
        if (newValue) {
            iElement.append(
                profileFieldService
                    .getTemplate(scope.ersProfileEditableField.type, scope)
            );
            $compile(iElement.contents())(scope);
        };
    });
}

The watcher waits for the roles data that is fetched from the server before doing the compile. It uses a one-time binding so that the compile is performed only once but the ng-repeat directive will continue to update the DOM as the value of roles changes.

1 Comment

that was the mistake i have updated but it is not the answer

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.