{"id":3286,"date":"2014-11-26T10:08:27","date_gmt":"2014-11-26T02:08:27","guid":{"rendered":"https:\/\/sdeno.com\/?p=3286"},"modified":"2014-11-26T10:08:43","modified_gmt":"2014-11-26T02:08:43","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e6%96%87%e6%9c%ac%e8%a1%a8%e5%8d%95%e8%8e%b7%e5%8f%96%e7%84%a6%e7%82%b9","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=3286","title":{"rendered":"\u5982\u4f55\u4f7f\u7528angularjs\u5728\u6587\u672c\u8868\u5355\u83b7\u53d6\u7126\u70b9"},"content":{"rendered":"<pre>Name: &lt;input type=\"text\" focus-me=\"shouldBeOpen\"&gt;<\/pre>\n<pre>app.directive('focusMe', function($timeout, $parse) {\r\n return {\r\n \/\/scope: true, \/\/ optionally create a child scope\r\n link: function(scope, element, attrs) {\r\n var model = $parse(attrs.focusMe);\r\n scope.$watch(model, function(value) {\r\n console.log('value=',value);\r\n if(value === true) { \r\n $timeout(function() {\r\n element[0].focus(); \r\n });\r\n }\r\n });\r\n \/\/ to address @blesh's comment, set attribute value to 'false'\r\n \/\/ on blur event:\r\n element.bind('blur', function() {\r\n console.log('blur');\r\n scope.$apply(model.assign(scope, false));\r\n });\r\n }\r\n };\r\n});<\/pre>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/14833326\/how-to-set-focus-on-input-field\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/14833326\/how-to-set-focus-on-input-field<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Name: &lt;input type=&#8221;text&#8221; focus-me=&#8221;shouldBeOpen&#8221;&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-3286","post","type-post","status-publish","format-standard","hentry","category-angularjs"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/3286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3286"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/3286\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}