随身笔记
随身笔记

如何使用angularjs在文本表单获取焦点

Name: <input type="text" focus-me="shouldBeOpen">
app.directive('focusMe', function($timeout, $parse) {
 return {
 //scope: true, // optionally create a child scope
 link: function(scope, element, attrs) {
 var model = $parse(attrs.focusMe);
 scope.$watch(model, function(value) {
 console.log('value=',value);
 if(value === true) { 
 $timeout(function() {
 element[0].focus(); 
 });
 }
 });
 // to address @blesh's comment, set attribute value to 'false'
 // on blur event:
 element.bind('blur', function() {
 console.log('blur');
 scope.$apply(model.assign(scope, false));
 });
 }
 };
});

http://stackoverflow.com/questions/14833326/how-to-set-focus-on-input-field

随身笔记

如何使用angularjs在文本表单获取焦点
Name: <input type="text" focus-me="shouldBeOpen"> app.directive('focusMe', function($timeout, $parse) { return { //scope: true, // optionally create a child scope …
扫描二维码继续阅读
2014-11-26