Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS - Load a template html from directive on click of a button

I am trying to load a HTML template when a link is clicked. I have made a directive that contains templateUrl which loads a HTML file. I am calling a function when a link is clicked that appends a div with our custom directive "myCustomer" to a div already in index.html. whatever i have done so far is shown below, but it doesn't work.

index.html

    <!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example12-production</title>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
  <script src="script.js"></script>
</head>
<body ng-app="docsTemplateUrlDirective">
  <div ng-controller="Controller">
  <a href="#" ng-click="showdiv()">show</a>
  <div id="d"></div>
</div>
</body>
</html>

script.js

(function(angular) {
  'use strict';
angular.module('docsTemplateUrlDirective', [])
  .controller('Controller', ['$scope', function($scope) {

    $scope.showdiv = function(){
      $("#d").append("<div my-Customer></div>");
    };
  }])
  .directive('myCustomer', function() {
    return {
      templateUrl: 'my-customer.html'
    };
  });
})(window.angular);

my-customer.html

<p>DIV CONTENT</p>

Here is the link i was testing this code here

like image 785
Vikram Singh Jadon Avatar asked Jun 21 '15 17:06

Vikram Singh Jadon


People also ask

Can I use HTML template in angular?

Angular HTML templates provide a structured way of binding data exposed by Angular components to the end-user. For instance, this example shows making a drop-down calendar with particular controls. Think of it like any other input form control in that it is going to render the UI and provide the backing logic.

What does $compile do in Angularjs?

Overview. Compiles an HTML string or DOM into a template and produces a template function, which can then be used to link scope and the template together. The compilation is a process of walking the DOM tree and matching DOM elements to directives. Note: This document is an in-depth reference of all directive options.

Which directive can have a template in angular?

Component is the only directive which accepts template as HTML.


3 Answers

This is because the angular doesn't bind the directives if you append content like this,

you need to $compile service to do this and this will bind the directives against your $scope

so controller like,

.controller('Controller', ['$scope', '$compile', function($scope, $compile)     {

    $scope.showdiv = function(){
      var compiledeHTML = $compile("<div my-Customer></div>")($scope);
      $("#d").append(compiledeHTML);
    };

}])

here is the DEMO


OR good to do like this,

use ng-if to create or removing element from html,

try this code

Controller,

....
$scope.anableCustomerDirective = false;
$scope.showdiv = function(){
  $scope.anableCustomerDirective = true;
};

HTML

<body ng-app="docsTemplateUrlDirective">
  <div ng-controller="Controller">
      <a href="#" ng-click="showdiv()">show</a>
      <div id="d">
          <div my-Customer ng-if='anableCustomerDirective'></div>
      </div>
 </div>
</body>

Suggestion

if your main intention is to place a html content after the click, then you can use ng-include here and it will much cleaner and no need of a another directive.

<div id="d">        
    <div ng-include="templateURL"></div>
 </div>

 $scope.showdiv = function(){
      $scope.templateURL = 'my-customer.html';
 };

find the DEMO

like image 116
Kalhan.Toress Avatar answered Oct 20 '22 07:10

Kalhan.Toress


Seems like directive is only meant to load template from it.I'd suggest you to use ng-include directive then

Markup

<a href="#" ng-click="showDiv=true">show</a>
<div id="d"></div>
<div ng-include="showDiv ? 'my-customer.html': ''">
like image 23
Pankaj Parkar Avatar answered Oct 20 '22 05:10

Pankaj Parkar


The most common way is to use $compile. More info: Dynamically add directive in AngularJS

Then your controller may look like this:

angular.module('docsTemplateUrlDirective', [])
  .controller('Controller', ['$scope', '$compile', function($scope, $compile) {

    $scope.showdiv = function(){
        var el = $compile( "<div my-customer></div>" )( $scope );
        $('#d').append( el );
    };
  }]);

Note that the directive invokation should look like this: <div my-customer>, not <div my-Customer></div> as you have in your code.

like image 1
Thomas Weglinski Avatar answered Oct 20 '22 06:10

Thomas Weglinski