简体   繁体   English

AngularJS-图片“加载”事件

[英]AngularJS - Image “onload” event

I've been searching for an answer to simple but not trivial question: What is a right way to catch image' onload event in Angular only with jqLite? 我一直在寻找一个简单但并非琐碎的问题的答案:仅使用jqLit​​e在Angular中捕获图像的onload事件的正确方法是什么? I found this question , but I want some solution with directives. 我找到了这个问题 ,但是我想要一些指令解决方案。
So as I said, this is not accepted for me: 因此,正如我所说,这对我来说是不可接受的:

.controller("MyCtrl", function($scope){
    // ...
    img.onload = function () {
        // ...
    }

because it is in controller, not in directive. 因为它在控制器中,而不在指令中。

Here's a re-usable directive in the style of angular's inbuilt event handling directives: 这是angular内置事件处理指令样式的可重用指令:

angular.module('sbLoad', [])

  .directive('sbLoad', ['$parse', function ($parse) {
    return {
      restrict: 'A',
      link: function (scope, elem, attrs) {
        var fn = $parse(attrs.sbLoad);
        elem.on('load', function (event) {
          scope.$apply(function() {
            fn(scope, { $event: event });
          });
        });
      }
    };
  }]);

When the img load event is fired the expression in the sb-load attribute is evaluated in the current scope along with the load event, passed in as $event. 触发img load事件时,将在当前作用域中将sb-load属性中的表达式与load事件一起评估,并作为$ event传入。 Here's how to use it: 使用方法如下:

HTML 的HTML

<div ng-controller="MyCtrl">
  <img sb-load="onImgLoad($event)">
</div>

JS JS

  .controller("MyCtrl", function($scope){
    // ...
    $scope.onImgLoad = function (event) {
        // ...
    }

Note: "sb" is just the prefix I use for my custom directives. 注意:“ sb”只是我用于自定义指令的前缀。

Ok, jqLite' bind method doing well its job. 好的,jqLit​​e的bind方法表现出色。 It goes like this: 它是这样的:

We are adding directive' name as attribute in our img tag . 我们在img标签中添加指令名称作为属性。 In my case , after loading and depending on its dimensions , image have to change its class name from "horizontal" to "vertical" , so directive's name will be "orientable" : 就我而言,图像加载后并根据其尺寸,必须将其类名称从“水平”更改为“垂直”,因此指令的名称将是“可定向的”:

<img ng-src="image_path.jpg" class="horizontal" orientable />

And then we are creating simple directive like this: 然后我们创建如下的简单指令:

var app = angular.module('myApp',[]);

app.directive('orientable', function () {       
    return {
        link: function(scope, element, attrs) {   

            element.bind("load" , function(e){ 

                // success, "onload" catched
                // now we can do specific stuff:

                if(this.naturalHeight > this.naturalWidth){
                    this.className = "vertical";
                }
            });
        }
    }
});

Example (explicit graphics!): http://jsfiddle.net/5nZYZ/63/ 示例(显式图形!): http : //jsfiddle.net/5nZYZ/63/

AngularJS V1.7.3 Added the ng-on-xxx directive: AngularJS V1.7.3添加了ng-on-xxx指令:

<div ng-controller="MyCtrl">
  <img ng-on-load="onImgLoad($event)">
</div>

AngularJS provides specific directives for many events, such as ngClick , so in most cases it is not necessary to use ngOn . AngularJS为许多事件(例如ngClick提供了特定的指令,因此在大多数情况下,没有必要使用ngOn However, AngularJS does not support all events and new events might be introduced in later DOM standards. 但是,AngularJS并不支持所有事件,以后的DOM标准中可能会引入新的事件。

For more information, see AngularJS ng-on Directive API Reference . 有关更多信息,请参阅《 AngularJS ng-on指令API参考》

you can call javascript version of onload event in angular js. 您可以在angular js中调用onload事件的javascript版本。 this ng-load event can be applied to any dom element like div, span, body, iframe, img etc. following is the link to add ng-load in your existing project. 此ng-load事件可以应用于任何dom元素,例如div,span,body,iframe,img等。以下是在现有项目中添加ng-load的链接。

download ng-load for angular js 下载angular-js的ng-load

npm i angular-ng-load

Following is example for iframe, once it is loaded testCallbackFunction will be called in controller 以下是iframe的示例,加载后将在控制器中调用testCallbackFunction

EXAMPLE

JS JS

    // include the `ngLoad` module
    var app = angular.module('myApp', ['ngLoad']);
    app.controller('myCtrl', function($scope) {
        $scope.testCallbackFunction = function() {
          //TODO : Things to do once Element is loaded
        };

    });  

HTML 的HTML

  <div ng-app='myApp' ng-controller='myCtrl'> 
      <iframe src="test.html" ng-load callback="testCallbackFunction()">  
  </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM