search

Home  >  Q&A  >  body text

angular.js - custom directives in angular

minesweeper.directive('ngRightClick', function($parse) {
    return function(scope, element, attrs) {
        var fn = $parse(attrs.ngRightClick);
            element.bind('contextmenu', function(event) {
                scope.$apply(function() {
                    event.preventDefault();
                    fn(scope, {$event:event});
                });
        });
    };
});

The above code explains that it is a custom ngRightClick instruction, but first of all I can’t understand its meaning. Shouldn’t a custom instruction be in the following form:

minesweeper.directive("mineGrid",function(){
    return {
        restrict:'E',
        replace:false,
        templateUrl:'./templates/mineGrid.html'
    }
});

Please give me some advice

phpcn_u1582phpcn_u15822767 days ago1296

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-27 17:46:42

    First of all, the custom right-click is not a custom element and should not be constrained to element, let alone HTML. The direct return function is the abbreviation of the direct return link function, and the custom right button should be constrained to an attribute.

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-27 17:46:42

    This is using angular’s ​​built-in service $parse. It is recommended to read this article AngularJS insider detailed explanation Directive

    reply
    0
  • Cancelreply