0

How use controller variable inside a directive . I used popoverHtml inside scope in directive but when i add type like this type not work :

like this : scope: { popoverHtml:'@', type: @ },

my html is :

 good <input type='radio' name='type' value='good' data-ng-model='type' 
data-ng-change='change(type)' />
    bad <input type='radio' name='type' value='bad' data-ng-model='type'
 data-ng-change='change(type)' />
        <next-level id='pop' popover-html='{{typeMessage}}'></next-level>

my controller is :

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.typeMessage = 'PopOverTest';
  $scope.type = false;
  $scope.change = function(type){
    if(type == 'good'){
      $scope.typeMessage = 'very good';
    }else if(type == 'bad'){
      $scope.typeMessage = 'very bad';
    }
  };
});

my directive :

    app.directive('nextLevel', function () {
            return {
                restrict: 'EA',
                scope:{ popoverHtml:'@'},
           template: '<a ui-sref="register" tabindex="0" linkdisabled="{{type}}" 
 class="btn btn-block btn-success ng-class: {disabled: !(type)}" role="button" >next</a>',
                link: function (scope, el, attrs){
                  $(el[0]).popover({
                        trigger: 'click',
                        html: true,
                        toggle:'popover',   
                        title: 'notice !!',
                        content: scope.popoverHtml,  // Access the popoverHtml html
                        placement: 'bottom'
                    });

                  attrs.$observe('popoverHtml', function(val){
                    $(el[0]).popover('hide');
                    var popover = $(el[0]).data('bs.popover');
                     popover.options.content = val;
                    console.log(popover); 
                  })


                } 
            };   
        });

demo : https://plnkr.co/edit/wfC4DrTIp8fRIs6hEEDa?p=preview

3
  • why don't use ui-bootstrap popover? Commented Oct 2, 2016 at 16:45
  • strongly suggest you get rid of bootstrap.js and use angular-ui-bootstrap. Don't try to re-invent the wheel Commented Oct 2, 2016 at 16:46
  • It's working for me. Am I missing something? Commented Oct 2, 2016 at 17:13

1 Answer 1

1

You cannot send type as @ binding to the directive, if you want to use it inside ng-class. If you do changes to type in main controller, they will not be reflected in ng-class inside the directive. Take look here.

In order to reflect changes in ng-class you have to pass it as an = binding.

Plunker here

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

16 Comments

Can you try changing name when you pass it to directive. I have named it as button. Do something similar
I dont think its is a problem with AngularJS
Can you show your current html, directive, controller code?
In the plunker you have provided you have <next-level id='pop' popover-html='{{typeMessage}}' button='button'></next-level> . Is this exact line of code you have in your actual code? I see you are trying to pass button object to directive, but I don't see any $scope.button object in mainCtrl. I only see $scope.buy..
How does your <next-level id='pop' HTML look right now? Can you show it? Are you able to see button right now in your UI. Plunker you have provided does not show button..
|

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.