Ir al contenido principal

AngularJS: Directive

Directive

Register directive with reserved word "directive"

Simple Directive




app.directive('myDirective', function($compile) {
return {
restrict: 'E',
link : function(scope, element, attrs, controller) {
var markup = "<input type='text' ng-model='test' /> {{test}} <br/>";
angular.element(element).html(markup);
$compile(element.contents())(scope);
}
};
});

Include Directive 

  • To add the directive Angular is Camel sensitive
  • In the html include the directive in a tag

HTML

<my-directive/>


Include Directive 

  • The elements for a directive can be:
    • E - element
    • A- attribute
    • C-class
    • M - comments


app.directive('myDirective', function($compile) {
return {
restrict: 'E',
template: "<input type='text' ng-model='test' /> {{test}} <br/>"
};
});


HTML

<my-directive/>

Include Directive as class

  • In this way you can repeat the directive many times
app.directive('myDirective', function($compile) {
return {
restrict: 'C',
template: "<input type='text' ng-model='test' /> {{test}} <br/>"
};
});
  • Add in HTML

HTML

<div class="role"></div>


Include Directive as HTML & avoiding a specific tag


app.directive('role', function(){
     
      return {
      restrict: 'E',
      replace: false,
      templateUrl : '/html/service/directives/case.html'

      }
     
});


Directive with own Scope

  • The scope apply only for the directive
app.directive('myDirective', function($compile) {
return {
restrict: 'C',
templateUrl: "/js/template/directive1.js",
scope: {
}
};
});
HTML
<div  class='my-directive'></div>
<div  class='my-directive'></div>
<div  class='my-directive'></div>


Directive with own Scope passing parameters 

  • Directive
app.directive('myDirective', function($compile) {
return {
restrict: 'E',
templateUrl: "/js/template/directive1.js",
scope: {
dirAttribute: "=myAttribute"
}
};
});
  • Template

<input type='text' ng-model='dirAttribute' /> {{dirAttribute}} <br/>
  • HTML
<my-directive my-attribute="message" />
  • Also can be use to bind 
    • @ (pass as a string)
    • = (pass as a integer)
    • &


Directive with own Scope passing FUNCTIONS (Isolate scope)

  • Controller
p.directive('myDirective', function($compile) {
return {
restrict: 'E',
  • Directive
  • To associate function should use &
app.directive('myDirective', function() {
return {
restrict: 'E',
replace: true,
templateUrl: "/js/template/directive1.html",
scope: {
upvote: "&"
}
};
});
  • Template
<div>
<input type='text' ng-model='dirAttribute' /> {{dirAttribute}} <br>
<div ng-click="upvote()">Isolate</div>
<br />
</div>
  • HTML
<my-directive  upvote="click()"/>


    Directive to react with changes using observe

    • Directive
    app.directive('myDirective', function() {
    return {
    restrict: 'E',
            template: '<img />',
            replace: true,
            link: function(scope, element, attrs, controller) {
                attrs.$observe('attr', function(newValue, oldValue) {
                console.log(newValue);
                });
            }
    };
    });
    • HTML
    • When you type a new value in the input the directive will detect
    <div>
    <input type='text' ng-model='message' /> {{message}} <br />
    </div>
    <my-directive attr="{{message}}" />


    Directive with his own Controller

    app.directive('showTab', function () {
        return {
            restict: 'E',
            replace: true,
            template: "<button ng-click='sayHello()'></button>",
             controller: function($scope {
           $scope.sayHello = function(){
           alert('Hello');
     }
    
        };
    });
    • HTML
    <showTab/>


    Directive with External Controller

    app.directive('showTab', function () {
        return {
            restict: 'E',
            replace: true,
            template: "<button ng-click='sayHello()'></button>",
             controller: '@',
             name: 'ctrl'
     }
    
        };
    });

    • HTML
    <showTab ctrl="MyController"/>

    • Controller

    .app.controller('MyController', .....

    Associate directives inside other directives

    app.directive('myDirective', function() {
    return {
    restrict: 'E',
    replace: true,
    template: "<button ng-click='message()'>Adding a message </button>",
            controller: function($scope){
            var array = ['m1'];
            
            $scope.message = function(){
            alert(array.join() );
            }
            this.addingMessage = function(newMessage){
            array.push(newMessage);
            }        
            }
    };
    }).directive('extraMessage', function() {
    return{
    restrict: 'A',
    require: 'myDirective',
    link: function(scope, element, attrs, controller){
    controller.addingMessage('m2');
    }
    }).directive('extraMessage2', function() {
    return{
    restrict: 'A',
    require: 'myDirective',
    link: function(scope, element, attrs, controller){
    controller.addingMessage('m3');
    }
    }
    });

    • HTML
    <my-directive extra-message />



    Associate directives inside other directives with priority

    • Use priority to specify the order
    • Use terminal to avoid other directives with lower priority 

    
    
    app.directive('myDirective', function() {
    return {
    restrict: 'E',
    replace: true,
    priority: 1,
    templateUrl: "/js/template/directive1.html",
            controller: function($scope){
            var array = ['m1'];
            $scope.message = function(){
            alert(array.join() );
            }
            this.addingMessage = function(newMessage){
            array.push(newMessage);
            }
            
            }
    };
    }).directive('extraMessage', function() {
    return{
    restrict: 'A',
    priority: 1,
    terminal: false,
    require: 'myDirective',
    link: function(scope, element, attrs, controller){
    controller.addingMessage('m2');
    }
    }
    }).directive('extraMessage2', function() {
    return{
    restrict: 'A',
    priority: 2,
    require: 'myDirective',
    link: function(scope, element, attrs, controller){
    controller.addingMessage('m3');
    }
    }
    });

    • HTML
    <my-directive extra-message2 extra-message />


    Nesting Directives

    • Add ^ to consider the nested directive
    • Add transclude in the main directive 
      transclude: true,
    • In the template add ng-transclude inside a <div>

    app.directive('myDirective', function() {
    return {
    restrict: 'E',
    replace: true,
    transclude: true,
    templateUrl: "/js/template/directive1.html",
            controller: function($scope){
            var array = ['m1'];
            $scope.message = function(){
            alert(array.join() );
            }
            this.addingMessage = function(newMessage){
            array.push(newMessage);
            }
            }
    };
    }).directive('extraMessage', function() {
    return{
    restrict: 'A',
    require: '^myDirective',
    link: function(scope, element, attrs, controller){
    controller.addingMessage('m2');
    }
    }
    }).directive('extraMessage2', function() {
    return{
    restrict: 'A',
    require: '^myDirective',
    link: function(scope, element, attrs, controller){
    controller.addingMessage('m3');
    }
    }
    });

    • HTML
    <my-directive> 
    <div extra-message extra-message2 />
    </my-directive> 

    • Template
    <div>
    <button ng-click='message()'>Adding a message </button>
    <div ng-transclude></div>
    </div>



    Transclude

    This directive add external content to the Directive

    app.directive('collapse', function() {
    return {
    restrict: 'E',
    replace: true,
    templateUrl: "/js/template/directive2.html",
    transclude: true,
    scope:{
    message: '@'
    }
    };
    });

    • HTML
    <collapse message="{{message}}">
    <p> including external message</p>
    </collapse>

    • Template
    <div>
    <h3>{{message}}</h3>
    <div ng-transclude></div>
    </div>


    Directive using compile

    This example is an input that accept a text to be repeated 5 times inside in a directive

    • HTML
    <input type="text" ng-model="num">
    <div repeat-x="5">{{num}}</div>

    • Returning a scope
    app.directive('repeatX', function($compile) {
    return {
    link: function(scope, element, attributes, controller){
    for(var i=1; i< Number(attributes.repeatX); i++){
    element.after($compile(element.clone().attr('repeat-X', 0)) (scope));
    }
    }
    };
    });
    • Efficient way
    • In this case the scope is individual
    app.directive('repeatX', function($compile) {
    return {
    compile: function( element, attributes){
    for(var i=1; i< Number(attributes.repeatX); i++){
    element.after(element.clone().attr('repeat-X', 0));
    }
    }
    };
    });


    • Returning a link function
    app.directive('repeatX', function($compile) {
    return {
    compile : function(element, attributes) {
    for (var i = 1; i < Number(attributes.repeatX); i++) {
    element.after(element.clone().attr('repeat-X', 0));
    }
    return function(scope, element, attributes, controller) {
    attributes.$observe('text', function(newValue) {
    if (newValue === 'change') {
    element.css('color', 'red');
    }
    })
    }
    }
    };
    });

    • HTML
    <input type="text" ng-model="num">
    <div repeat-x="5" text='{{num}}'>{{num}}</div>

    AngularJS & JQuery


    • Add Jquery in the easiest way
      • Add the event to the element

    • HTML
    <input id="event" type="text">
    <script type="text/javascript">
    $('#event').datepicker();
    </script>


    Directive executing JQuery

    app.directive('datePicker', function($compile) {
    return {
    restrict: 'A',
    link: function(scope, element){
    element.datepicker();
    }
    };
    });

    • HTML
    <div>
    <input id="event" date-picker type="text">
    </div>













    app.directive('showTab', function () {
        return {
            link: function (scope, element, attrs) {
                element.click(function (e) {
                    e.preventDefault();
                    jQuery(element).tab('show');
                });
            }
        };
    });
    
    <a show-tab href="#tab_1">
        Tab 1
    </a>






    Comentarios

    Entradas populares de este blog

    C# Using tabs

    To use tabs in C# use the TabContainer element from AjaxControlToolkit Include AjaxControlToolkit  Include in the Web.config file, inside the tag <system.web> the following code  <pages>       <controls>         <add tagPrefix="ajaxCTK" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>       </controls>     </pages>   Include TabContainer element First  include TabContainer element that is the section where all the tabs will be displayed. <ajaxCTK:TabContainer ID="TabContainerUpdate" runat="server"                 Height="800"                 CssClass="ajax__tab_style"> </ajaxCTK:TabContainer> Second per each tab include the following code corresponding to each ...

    Rails - Basic Steps III

    pValidations Validations are a type of ActiveRecord Validations are defined in our models Implement Validations Go to   root_app/app/models Open files  *.rb for each model Mandatory field validates_presence_of   :field Ex:   validates_presence_of    :title Classes The basic syntax is class MyClass        @global_variable                def my_method              @method_variable        end end Create an instance myInstance = MyClass.new Invoke a mehod mc.my_method class() method returns the type of the object In Ruby, last character of method define the behavior If ends with a question -> return a boolean value If ends with an exclamation -> change the state of the object Getter / Setter method def global_variable       return @global_variable end ...

    Python create package

    Create a root folder Create a sub-folder "example_pkg" that contains the funtionallity packaging_tutorial/ example_pkg/ __init__.py In the root folder create the following structure  packaging_tutorial/ example_pkg/ __init__.py tests/ setup.py LICENSE README.md in the setup.py contains the configuration of the packages your package is found by find_packages() import setuptools with open ( "README.md" , "r" ) as fh : long_description = fh . read () setuptools . setup ( name = "example-pkg-YOUR-USERNAME-HERE" , # Replace with your own username version = "0.0.1" , author = "Example Author" , author_email = "author@example.com" , description = "A small example package" , long_description = long_description , long_description_content_type = "text/markdown" , url = "https://github.com/pypa/sam...