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" />