2

suppose i have two button and when we click on button 1 then show function call in controller and same way when we click on button 2 then hide function call in controller.

from show function how could i dynamically load and add directive in page and as well as how could i hide or remove directive from page when hide function will be called.

hence i am new in angular so not sure the below code will add directive at run time if i call it from show function ?

$('body').append($compile("<my-angular-directive />")(scope));
scope.$apply(); 

i do not know how to remove directive from page if it exist in page from controller function. give me suggestion how to achieve this. thanks

3 Answers 3

4

you can use directive as class and you can load it with ng-class directive

var jimApp = angular.module("mainApp",  []);

jimApp.controller('mainCtrl', function($scope){
  $scope.showMyDir = true;
  $scope.buttonClcik = function(){
    $scope.showMyDir = !$scope.showMyDir;
  };
  
});

jimApp.directive("customDir1", function() {
  return {
    restrict:"AEC",
    scope:{
      value:"="
    },
    link: function(scope, element, attrs) {
    }
  }
});

jimApp.directive("customDir2", function() {
  return {
    restrict:"C",
    link: function(scope, element, attrs) {
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainCtrl">
  <button ng-click="buttonClcik();">Click</button>
  
  <div  ng-class="{'custom-dir1': showMyDir, 'custom-dir2': !showMyDir}" value="showMyDir">Hai</div>
</div>

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

4 Comments

i just want to load and inject directive on button click.
if possible show me how can we use ng-class to load directive. i use ng-class to work with css class.
@MonojitSarkar don't use ng-if, ng-if is work like an $watch it will affect on your app performance
how could i add directive to page dynamically on button click and append function......is it possible?
3

Use ng-if. So basically if you have a scope variable let's say $scope.showElement you can then use it like this:

In your controller:

$scope.showElement = true;

HTML:

<this-directive ng-if="showElement"></this-directive>

Originally the directive will show up but when you change the value of $scope.showElement it will be removed.

EDIT based on your comment:

Set your $scope.showElement to false and on ng-click set it to true like this:

angular.module('myApp', [])

.controller('testController', function($scope) {
  
  $scope.showElement = false;

  $scope.toggleElement = function() {
    console.log('toggle element');
    $scope.showElement = $scope.showElement ? false : true;
  };
  
});
<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
  </head>
  <body ng-controller="testController">
     <button type="button" ng-click="toggleElement()">Show directive</button>
     <div ng-if="showElement" style="width:100%;height:40px;background-color:red;">This element will be shown/removed</div>
  </body>
</html>

EDIT 2:

You can also use css declarations for better performance if you don't mind that the element still stays on the page but is hidden. With css it would go like this:

angular.module('myApp', [])

.controller('testController', function($scope) {
  
  $scope.showElement = false;

  $scope.toggleElement = function() {
    console.log('toggle element');
    $scope.showElement = $scope.showElement ? false : true;
  };
  
});
.hidden {
  display: none;
}
<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
  </head>
  <body ng-controller="testController">
     <button type="button" ng-click="toggleElement()">Show directive</button>
     <div ng-class="{'hidden' : showElement === false}" style="width:100%;height:40px;background-color:red;">This element will be shown/removed</div>
  </body>
</html>

2 Comments

how could i add directive to page dynamically on button click and append function......is it possible?
I would not recommend that and I think you need to do a lot to make it work. Ng-if is for situations like that, why would you need to specifically need to append it to the DOM?
1

You could easily use ng-if directive on the wrapper of your custom directive

<div ng-if="showMyDir()">
  <my-angular-directive />
</div>

<div ng-if="showMyDirTwo()">
  <my-angular-directive-two />
</div>

1 Comment

how could i add directive to page dynamically on button click and append function......is it possible?

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.