AngularJS:如何收听DOM事件?

前端之家收集整理的这篇文章主要介绍了AngularJS:如何收听DOM事件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是AngularJS的新手,请原谅我这个转储问题.
我如何收听’click’或’mousemove’等’dom’事件?

这就是我得到的(没有错误,但也没有导致控制台)

//代码基于原始angularjs-seed.

angular.module('myApp.controllers',[]).
  controller('MyCtrl1',['$scope',function($scope) {

        $scope.$on('dragover',function() {
            console.log('dragover');
        });

        $scope.$on('click',function() {
            console.log('click');
        });

  }])

  .controller('MyCtrl2',[function() {

  }]);
在AngularJS中,事件通常由 directives处理.

Directives are a way to teach HTML new tricks. During DOM compilation
directives are matched against the HTML and executed. This allows
directives to register behavior,or transform the DOM.

对于“click”事件,您将使用ngClick指令:

HTML:

<button ng-click="doSomething()">Click me</button>

JS:

function MyCtrl($scope){
  $scope.doSomething = function(){
    // do something...
  };
}

对于“dragover”事件(以及Angular本机指令尚未涵盖的其他事件),您可以编写自己的指令:

HTML:

<div drop-target>Drop here</div>

JS:

angular.module('MyApp')
  .directive('dropTarget',function(){
    return function($scope,$element){
      $element.bind('dragover',function(){
        // do something when dragover event is observed
      });
    };
  })
原文链接:https://www.f2er.com/angularjs/143893.html

猜你在找的Angularjs相关文章