LoginSignup
3
3

More than 5 years have passed since last update.

AngularJS on-load-element

Last updated at Posted at 2013-08-02

jQueryのプラグイン使うときに、モデルの値が読み込まれてからjQueryプラグイン適用したい場合があったので、directiveでやってみることに。

sampleはこちら

angular-at-on-load-element
###*
 * angular at-on-load-element
 *
 * @author atomita
 * @license MIT
 * @version 0.0.1
###
angular.module("at-on-load-element", []).directive("atOnLoadElement", ["$timeout", ($timeout)->
  {
    restrict: "AC"
    scope: true
    link: (scope, iElement, iAttrs)->
      apply = iAttrs.atOnLoadElement
      if apply
        scope.element = iElement
        scope.attrs = iAttrs
        scope.angular = angular
        $timeout -> scope.$apply(apply)
      return
  }
]).directive("atOnLoadElementFromFirst", ["$timeout", ($timeout)->
  {
    restrict: "AC"
    scope: true
    link: (scope, iElement, iAttrs)->
      apply = iAttrs.atOnLoadElementFromFirst
      $parent = scope
      loop
        $parent = $parent.$parent
        if $parent
          if "$first" of $parent
            $first = $parent.$first
            break
        else
          break

      if apply and $first
        scope.element = iElement
        scope.attrs = iAttrs
        scope.angular = angular
        $timeout -> scope.$apply(apply)
      return
  }
]).directive("atOnLoadElementFromMiddle", ["$timeout", ($timeout)->
  {
    restrict: "AC"
    scope: true
    link: (scope, iElement, iAttrs)->
      apply = iAttrs.atOnLoadElementFromMiddle
      $parent = scope
      loop
        $parent = $parent.$parent
        if $parent
          if "$middle" of $parent
            $middle = $parent.$middle
            break
        else
          break

      if apply and $middle
        scope.element = iElement
        scope.attrs = iAttrs
        scope.angular = angular
        $timeout -> scope.$apply(apply)
      return
  }
]).directive("atOnLoadElementFromLast", ["$timeout", ($timeout)->
  {
    restrict: "AC"
    scope: true
    link: (scope, iElement, iAttrs)->
      apply = iAttrs.atOnLoadElementFromLast
      $parent = scope
      loop
        $parent = $parent.$parent
        if $parent
          if "$last" of $parent
            $last = $parent.$last
            break
        else
          break

      if apply and $last
        scope.element = iElement
        scope.attrs = iAttrs
        scope.angular = angular
        $timeout -> scope.$apply(apply)
      return
  }
])
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3