element.focus在angularjs中不起作用

时间:2016-03-08 14:38:11

标签: javascript jquery angularjs

element.focus ID无效。即使价值是真的。

workSpaceModule.directive('focusMe', function($timeout, $parse) {
  return {

    link: function(scope, element, attrs) {
      var model = $parse(attrs.focusMe);
      scope.$watch(model, function(value) {
        console.log('value=',value);

        if(value === true) { 

          $timeout(function() {
            console.log('element=',element[0]);
            window.setTimeout(function ()
            {
                element[0].focus();
                 element[0].select();
            }, 0);
          });
        }
      });
      element.bind('blur', function() {
        console.log('blur')
        scope.$apply(model.assign($scope, false));
      })
    }
  };
});
// HTML
<input type="text" id="txtWorkspace"
ng-model="workSpace.name" class="form-control" 
placeholder="Name your workspace" 
ng-keyup="$event.keyCode == 13 && createWorkSpace()" 
focus-me="myfocus" />

0 个答案:

没有答案