angular 获取焦点指令

angular.module('app').directive('focus', function ($timeout,$parse) {
  return {
    restrict: 'A',
    link: function ($scope, $ele, $attrs, $controller) {
      let model = $parse($attrs.focus);
      let unBind=  $scope.$watch(model, function (newValue,oldValue) {
        if (newValue === true) {
          $timeout(function () {
            $ele[0].focus();
          });
        }
      });
      $ele.bind('blur', function () {
        $scope.$apply(model.assign($scope, false));
      });
      $scope.$on("destroy", () => {
        unBind();
      })
    }
  }
});

  

posted @ 2017-03-24 15:32  N1900  阅读(523)  评论(0编辑  收藏  举报