{"id":485,"date":"2013-09-09T00:15:41","date_gmt":"2013-09-08T16:15:41","guid":{"rendered":"http:\/\/www.storagelab.org.cn\/zhangdi\/?p=485"},"modified":"2013-09-09T00:15:41","modified_gmt":"2013-09-08T16:15:41","slug":"angularjs%e9%a1%b9%e7%9b%ae%e5%ae%9e%e6%88%98%ef%bc%8103%ef%bc%9aangularjs%e4%b8%8e%e5%85%b6%e4%bb%96%e7%b1%bb%e5%ba%93%e7%9a%84%e5%8d%8f%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/www.divis.cn\/index.php\/2013\/09\/09\/angularjs%e9%a1%b9%e7%9b%ae%e5%ae%9e%e6%88%98%ef%bc%8103%ef%bc%9aangularjs%e4%b8%8e%e5%85%b6%e4%bb%96%e7%b1%bb%e5%ba%93%e7%9a%84%e5%8d%8f%e4%bd%9c\/","title":{"rendered":"angularJs\u9879\u76ee\u5b9e\u6218\uff0103\uff1aangularjs\u4e0e\u5176\u4ed6\u7c7b\u5e93\u7684\u534f\u4f5c"},"content":{"rendered":"<h4>\u5f15\u8a00\uff1aangularjs\u662f\u4e00\u4e2a\u4e2d\u7b49\u91cd\u91cf\u7ea7\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6<\/h4>\n<p>HTML\u662f\u4e00\u95e8\u5f88\u597d\u7684\u4e3a\u9759\u6001\u6587\u672c\u8bbe\u8ba1\u7684\u8bed\u8a00\uff0c\u4f46\u8981\u6784\u5efa\u52a8\u6001\u7684web\u5e94\u7528\u5b83\u5c31\u663e\u7684\u4e4f\u529b\u4e86\u3002\u901a\u5e38\uff0c\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u6280\u672f\u6765\u89e3\u51b3\u9759\u6001\u7f51\u9875\u6280\u672f\u5728\u6784\u5efa\u52a8\u6001\u5e94\u7528\u4e0a\u7684\u4e0d\u8db3\uff1a<\/p>\n<p>1.\u7c7b\u5e93\uff1a\u7c7b\u5e93\u662f\u4e00\u7c7b\u51fd\u6570\u7684\u96c6\u5408\uff0c\u5b83\u80fd\u5e2e\u52a9\u4f60\u5199web\u5e94\u7528\u3002\u8fd9\u91cc\u8d77\u4e3b\u5bfc\u4f5c\u7528\u662f\u4f60\u7684\u4ee3\u7801\uff0c\u7531\u4f60\u6765\u51b3\u5b9a\u4f55\u65f6\u4f7f\u7528\u7c7b\u5e93\u3002\u5178\u578b\u7684\u7c7b\u5e93\uff0c\u4f8b\u5982prototype\u3001jquery\u7b49\u3002<\/p>\n<p>2.\u6846\u67b6\uff1a\u6846\u67b6\u5f0f\u4e00\u79cd\u7279\u6b8a\u7684\u3001\u5df2\u7ecf\u5b9e\u73b0\u7684web\u5e94\u7528\uff0c\u4f60\u53ea\u9700\u8981\u586b\u5145\u5177\u4f53\u7684\u4e1a\u52a1\u903b\u8f91\u3002\u8fd9\u91cc\u6846\u67b6\u662f\u8d77\u4e3b\u5bfc\u4f5c\u7528\u7684\uff0c\u7531\u5b83\u6839\u636e\u5177\u4f53\u7684\u903b\u8f91\u6765\u8c03\u7528\u4f60\u7684\u4ee3\u7801\u3002\u5178\u578b\u7684\u6846\u67b6\u4f8b\u5982knockout,sproutcore, YUI\u7b49\u3002angularjs\u4e5f\u662f\u5176\u4e2d\u4e4b\u4e00\u3002<\/p>\n<p>\u6846\u67b6\u53c8\u6709\u8f7b\u91cd\u4e4b\u5206\u3002\u6211\u5bf9\u8f7b\u91cd\u7684\u5224\u65ad\u6807\u51c6\u662f\uff0c\u662f\u5426\u9700\u8981\u5f88\u591a\u7684\u7b2c\u4e09\u65b9\u7c7b\u5e93\u6765\u5e2e\u52a9\u4f60\u5b9e\u73b0\u529f\u80fd\u3002\u663e\u7136\uff0cbackbone\u8fd9\u79cd\u5c5e\u4e8e\u8f7b\u91cf\u7ea7\u6846\u67b6\uff0c\u5b83\u7b80\u5355\u6613\u7528\uff0c\u4e13\u6ce8\u4e8e\u524d\u7aefMvc\u7684\u5b9e\u73b0\uff0c\u6545\u800c\u4f60\u8fd8\u9700\u8981\u5f88\u591a\u7b2c\u4e09\u65b9\u7c7b\u5e93\uff08\u81f3\u5c11jquery\uff09\u6765\u5b8c\u6210dom\u64cd\u4f5c\u3001UI\u7b49\u5404\u79cd\u5404\u6837\u7684\u5185\u5bb9\u3002Yui\u3001dojo\u5c5e\u4e8e\u91cd\u578b\u6846\u67b6\uff0c\u4ed6\u4eec\u7684\u4f5c\u8005\u4f01\u56fe\u641e\u51fa\u4e00\u4e2a\u68ee\u7f57\u4e07\u8c61\u7684\u6846\u67b6+\u7ec4\u4ef6\u5e93\uff0c\u5305\u62ec\u4ee3\u7801\u52a8\u6001\u8c03\u7528\u3001\u5404\u79cdUI\u7ec4\u4ef6\u90fd\u5305\u542b\u5728\u5185\uff0c\u5b66\u4e60\u6210\u672c\u8f83\u9ad8\uff0c\u4f46\u662f\u4e00\u65e6\u7cbe\u901a\uff0c\u81f3\u5c11\u8fd9\u4e2a\u9879\u76ee\u522b\u65e0\u6240\u6c42\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u8bb2\uff0c\u8f7b\u91cf\u7ea7\u6846\u67b6\u597d\u6bd4\u6bdb\u576f\u623f\uff0c\u8fd8\u9700\u8981\u5404\u79cd\u5de5\u5177\u505a\u88c5\u4fee\uff0c\u4f46\u662f\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\u4e5f\u66f4\u7075\u6d3b\u3002\u91cd\u91cf\u7ea7\u6846\u67b6\u597d\u6bd4\u7cbe\u88c5\u4fee\u7684\u623f\u95f4\uff0c\u4f60\u53ea\u9700\u8981\u7684\u662f\u9002\u5e94\u5b83\uff0c\u4f46\u5982\u679c\u8981\u81ea\u5df1\u505a\u51fa\u5927\u5200\u9614\u65a7\u7684\u4fee\u6539\uff0c\u90a3\u5c31\u7a0d\u5fae\u6709\u70b9\u4f24\u7ecf\u52a8\u9aa8\u4e86\u3002<\/p>\n<p>angularjs\uff0c\u5728\u6211\u770b\u6765\u662f\u4ecb\u4e8e\u4ee5\u4e0a\u4e24\u7c7b\u4e4b\u95f4\uff0c\u662f\u4e2a\u4e2d\u7b49\u91cd\u91cf\u7ea7\u7684\u6846\u67b6\u3002\u5373\u4e0d\u50cfbackbone\u90a3\u4e48\u7b80\u5355\uff0c\u4e5f\u4e0d\u50cfdojo\u548cYui\u90a3\u4e48\u5305\u7f57\u4e07\u8c61\u3002\u5f88\u591a\u65f6\u5019\uff0c\u5984\u56fe\u5305\u7f57\u4e07\u8c61\uff0c\u5f80\u5f80\u4f1a\u51fa\u73b0\u5f88\u591a\u5b50\u6a21\u5757\u7684\u8d28\u91cf\u9ad8\u4e0d\u6210\u4f4e\u4e0d\u5c31\uff0c\u5e76\u4e14\u4fee\u6539\u8d77\u6765\u8f83\u4e3a\u56f0\u96be\u3002\u8fc7\u5206\u7cbe\u7b80\uff0c\u5219\u6846\u67b6\u5185\u5bb9\u5355\u8584\u9700\u8981\u5199\u7684\u5185\u5bb9\u592a\u591a\u3002angularjs\u8fd9\u79cd\u76f8\u5bf9\u4e2d\u5eb8\u7684\u98ce\u683c\uff0c\u5219\u975e\u5e38\u7b26\u5408\u6211\u7684\u9700\u6c42\u3002\u76ee\u524d\uff0cAngularJS\u4e09\u4e2a\u6211\u8ba4\u4e3a\u6700\u4e3a\u7cbe\u5999\u7684\u7ec4\u4ef6\u5c31\u662f\u6570\u636e\u7ed1\u5b9a\uff08Scope\uff09\uff0c\u6307\u4ee4\uff08Directive\uff09\u548c\u4f9d\u8d56\u6ce8\u5165\uff08Dependency Injection\uff09\uff0c\u8868\u73b0\u5f97\u975e\u5e38\u597d\u3002\u76f8\u5bf9\u800c\u8a00\uff0c\u5b83\u7684UI\u7ec4\u4ef6\u548c\u52a8\u753b\u5219\u662f\u5f31\u9879\u3002\u53ef\u4ee5\u8bf4\uff0c\u9009\u62e9\u4e86angularjs\uff0c\u5c31\u610f\u5473\u7740\u9009\u62e9\u4e86jquery\u5f0f\u7684\u7ec4\u4ef6\u5e93\u65b9\u5f0f\u6765\u5f25\u8865\u5b83\u7684\u4e0d\u8db3\uff0c\u8981\u5b8c\u6210\u4e00\u4e2aweb\u5e94\u7528\u5fc5\u987b\u8ddf\u7b2c\u4e09\u65b9\u7c7b\u5e93\u6253\u4ea4\u9053\u3002<\/p>\n<p>\u73b0\u5728\u5df2\u7ecf\u6709\u8bb8\u591a\u9488\u5bf9angularjs\u5199\u7684UI\u63d2\u4ef6\uff0c\u6709\u7684\u662f\u7ed3\u5408\u4e86bootstrap\uff0c\u6709\u7684\u662f\u7ed3\u5408\u4e86jquery, \u867d\u7136\u4e0d\u592a\u5b8c\u5584\uff0c\u90fd\u5f88\u503c\u5f97\u53c2\u8003\uff1a<a href=\"http:\/\/angular-ui.github.io\/\">http:\/\/angular-ui.github.io\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<h4>\u4e0ejquery\u7c7b\u5e93\u7684\u534f\u4f5c<\/h4>\n<p>\u7b2c\u4e09\u65b9\u7c7b\u5e93\u4e2d\uff0c\u4e0d\u5f97\u4e0d\u63d0\u7684\u662f\u5927\u540d\u9f0e\u9f0e\u7684jquery,\u73b0\u5728\u57fa\u672c\u4e0a\u5df2\u7ecf\u662f\u56fd\u5185web\u5f00\u53d1\u7684\u5fc5\u4fee\u5de5\u5177\u4e86\u3002\u5b83\u7075\u6d3b\u7684dom\u64cd\u4f5c\uff0c\u8ba9\u5f88\u591aweb\u5f00\u53d1\u4eba\u5458\u6b32\u7f62\u4e0d\u80fd\u3002\u518d\u52a0\u4e0a\u5df2\u7ecf\u5f88\u6210\u719f\u7684jquery UI \u5e93\u548c\u5927\u91cfjquery \u63d2\u4ef6\uff0c\u51e0\u4e4e\u662f\u4e00\u4e2a\u53d6\u4e4b\u4e0d\u5c3d\u7528\u4e4b\u4e0d\u7aed\u7684\u5b9d\u5e93\u3002\u7136\u800c\uff0c\u5b83\u662f\u5426\u80fd\u4e0eangularjs\u7ed3\u5408\u5462\uff1f<\/p>\n<p>\u5f88\u591aangularjs\u539f\u6559\u65e8\u4e3b\u4e49\u8005\u5bf9\u6b64\u6301\u5426\u5b9a\u6001\u5ea6\u3002\u4ed6\u4eec\u8ba4\u4e3a\uff0c\u65e2\u7136\u5df2\u7ecf\u4f7f\u7528\u4e86angularjs\u505aweb\u5e94\u7528\u6846\u67b6\uff0c\u90a3\u5c31\u5fc5\u987b\u907f\u514d\u5176\u4ed6\u7c7b\u5e93\u7684\u5e72\u6270\uff0c\u505a\u7eaf\u51c0\u7684MvvM\u6a21\u5f0f\u5e94\u7528\u3002\u4efb\u4f55\u7c7b\u4f3cjquery\u7684dom\u64cd\u4f5c\uff0c\u90fd\u662f\u4e0d\u6d01\u7684\u3002\u628a\u6240\u6709\u548c\u754c\u9762\u76f8\u5173\u7684, \u6bd4\u5982dom\u64cd\u4f5c, \u90fd\u653e\u5728directive\u4e2d, \u8fd9\u6837\u9875\u9762\u4e2ddirective\u800c\u6ca1\u6709\u4ee3\u7801\uff0c\u8ddfJSF\u601d\u60f3\u4e00\u81f4\u3002MVVM,DSL,\u7ec4\u4ef6\u5316\u7684\u601d\u60f3\u8fd9\u624d\u662fweb\u7684\u8d8b\u52bf\u3002\u55ef\uff0c\u60f3\u6cd5\u5f88\u597d\uff0c\u539f\u6559\u65e8\u4e3b\u4e49\u8005\u60f3\u6cd5\u90fd\u662f\u8fd9\u4e48\u7eaf\u6d01\u3002\u4f46\u4e8b\u5b9e\u60c5\u51b5\u662f\uff0c\u4f7f\u7528\u4e86angularjs\u6211\u4eec\u5c31\u79bb\u4e0d\u5f00jquery\u3002<\/p>\n<p>\u4f17\u6240\u5468\u77e5\uff0cangularjs\u91cc\u9762\u4e8b\u5b9e\u4e0a\u5df2\u7ecf\u5185\u7f6e\u4e86jquery lite.\uff0c\u800c\u4e14angularjs\u6e90\u7801\u4e2d\u5f88\u591a\u65b9\u6cd5\u76f4\u63a5\u5c31\u662f\u4f7f\u7528jquery\u65b9\u6cd5\u3002\u4f8b\u5982angularjs\u7684\u4e8b\u4ef6\u7ed1\u5b9a\u673a\u5236\u3002\u65e2\u7136\u5148\u77e5\u4eec\u90fd\u5728\u7528\uff0c\u6211\u4eec\u53c8\u4f55\u82e6\u4e0d\u7528\uff1f\u7ec4\u4ef6\u5316\u7684\u601d\u60f3\u6ca1\u6709\u9519\uff0c\u4f46\u6ca1\u5fc5\u8981\u56e0\u6b64\u628a\u81ea\u5df1\u7684\u624b\u811a\u7ed1\u4f4f\u3002\u552f\u4e00\u8981\u6ce8\u610f\u7684\u95ee\u9898\u662f\uff0c\u4e0d\u8981\u7528jquery\u7684\u4ee3\u7801\u7834\u574f\u4e86angularjs\u7684\u7ed3\u6784\u3002\u5bf9\u6b64\u6211\u7684\u539f\u5219\u5982\u4e0b\uff0c\u4e0d\u8db3\u4e4b\u5904\u8fd8\u8bf7\u6307\u51fa\uff1a<\/p>\n<ol>\n<li><strong><span style=\"font-size: 13px;line-height: 19px\">\u6a21\u5757\u5212\u5206\u3001\u670d\u52a1\u3001\u8def\u7531\u3001\u4f9d\u8d56\u6ce8\u5165\u7b49\u91cd\u8981\u65b9\u9762\u4e0a\u90fd\u5f97\u4f7f\u7528angularjs\u7684\u65b9\u5f0f\uff0c\u53ea\u6709\u67d0\u4e9b\u5177\u4f53\u5185\u5bb9\uff08\u901a\u5e38\u662f\u4e00\u4e9bUi\uff09\u624d\u4f7f\u7528jquery\u3002<\/span><\/strong><\/li>\n<li><strong><span style=\"font-size: 13px;line-height: 19px\">\u907f\u514d\u5728controller\u91cc\u9762\u5199\u4e86\u4e00\u5806\u76f4\u63a5\u64cd\u4f5cdom\u5143\u7d20\u7684 jquery\u4ee3\u7801\u3002\u4f7f\u7528angularjs\u7684\u6a21\u677f\u7ed1\u5b9a\u673a\u5236\u3002<\/span><\/strong><\/li>\n<li><strong><span style=\"font-size: 13px;line-height: 19px\">\u5e38\u7528\u7684\u7ec4\u4ef6\u8981\u7528angularjs\u7684\u65b9\u6cd5\u62bd\u53d6\u51fa\u6765\uff0c\u4f46\u7ec4\u4ef6\u5177\u4f53\u5b9e\u73b0\u5219\u4e0d\u5fc5\u7ea0\u7ed3\u4e8e\u662f\u5426\u4f7f\u7528jquery\u53ca\u5176\u63d2\u4ef6\u3002<\/span><\/strong><\/li>\n<li><strong><span style=\"font-size: 13px;line-height: 19px\">.\u4f7f\u7528\u7b2c\u4e09\u65b9\u7c7b\u5e93\u65f6\uff0c\u5728\u53d8\u91cf\u548c\u51fd\u6570\u547d\u540d\u65f6\u6709\u7279\u6b8a\u6807\u8bb0\uff08\u901a\u5e38\u662f\u52a0\u4e0a\u8fd9\u4e2a\u7c7b\u5e93\u540d\u7684\u7f29\u5199\uff09\u3002<\/span><\/strong><\/li>\n<\/ol>\n<h4>jquery,\u66f4\u662f\u5efa\u8bae\u4f5c\u4e3aangularjs\u7684\u4f9d\u8d56\uff0c\u5148\u4e8eangularjs\u52a0\u8f7d\u8fdb\u6765\u3002<\/h4>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u9009\u62e9\u4e86angularjs\u8fd9\u6837\u7684\u6846\u67b6\u4e2d\u5fb7\u4e2d\u7b49\u91cd\u91cf\u7ea7\u9009\u624b\uff0c\u5c31\u610f\u5473\u7740\u4f60\u5fc5\u987b\u6dfb\u52a0\u5176\u4ed6\u7c7b\u5e93\u3002\u800cjquery,\u66f4\u662f\u5efa\u8bae\u4f5c\u4e3aangularjs\u7684\u4f9d\u8d56\uff0c\u5148\u4e8eangularjs\u52a0\u8f7d\u8fdb\u6765\u3002\u56e0\u4e3a\u5728\u67e5\u770bangularjs API\u7684\u65f6\u5019\uff0c\u6211\u5df2\u7ecf\u53d1\u73b0\uff0c\u5176\u4e2d\u8bb8\u591a\u529f\u80fd\uff0c\u4e8b\u5b9e\u4e0a\u662f\u4f9d\u8d56\u4e8ejquery\u7684\u3002\u5178\u578b\u7684\u4f8b\u5b50\uff0c\u5c31\u662f\u5b98\u7f51\u7684ng-blur\u6307\u4ee4\u3002<\/p>\n<pre>\u3008input type=\"text\" ng-model=\"name\" ng-blur=\"checkname()\" \u3009<\/pre>\n<p><span style=\"font-size: 13px;line-height: 19px\">ng-blur\u6307\u4ee4\uff0c\u662f\u5728\u7126\u70b9\u79bb\u5f00\u67d0\u4e2a\u5143\u7d20\u65f6\u89e6\u53d1\u7684\u6307\u4ee4\u3002\u5bf9\u4e8e\u4e0a\u4f8b\uff0c\u5373\u5728\u7126\u70b9\u79bb\u5f00\u8be5\u6587\u672c\u8f93\u5165\u6846\u65f6\uff0c\u89e6\u53d1checkname()\u51fd\u6570\u3002<\/span><\/p>\n<p>\u770b\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u4f46\u662f\u4f60\u5982\u679c\u771f\u7684\u4f7f\u7528\u4e86\u8fd9\u4e2a\u6307\u4ee4\uff0c\u4f60\u5c31\u4f1a\u53d1\u73b0\u5b83\u6839\u672c\u4e0d\u8d77\u6548\u679c\u3002\u5728\u4ed4\u7ec6\u67e5\u770b\u6587\u6863\u540e\uff0c\u6211\u624d\u53d1\u73b0\u8fd9\u5b9e\u9645\u662f\u5148\u77e5\u4eec\u4f7f\u7528jquery\u7684blur\u65b9\u6cd5\u5b9e\u73b0\u7684\u51fd\u6570\uff08\u800c\u4e14\u4e8b\u5b9e\u4e0a\u6839\u672c\u6ca1\u6709\u771f\u6b63\u5b9e\u73b0\u5e76\u653e\u5728\u5f53\u524d\u7684\u7248\u672c\u91cc\uff09\u3002\u90a3\u4e48\u5c31\u7b97\u6211\u4eec\u60f3\u5199\u4e00\u4e2a\uff0c\u79bb\u5f00jquery\u539f\u751f\u5e93\u662f\u4e0d\u884c\u7684,\u56e0\u4e3ablur\u65b9\u6cd5\u5e76\u672a\u5c01\u88c5\u5230angularjs\u5185\u5e26\u7684jquery lite\u91cc\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5fc5\u987b\u5148\u8f7d\u5165\u5b8c\u6574\u7684jquery\u624d\u80fd\u4f7f\u7528\u3002\u4e8e\u662f\uff0c\u6211\u5e72\u8106\u81ea\u5df1\u5199\u4e86\u4e00\u4e2a\u6807\u7b7e\uff1a<\/p>\n<pre>\/*\n * angular directive onBlur\n *\n * @description my ng-blur\n * @require jquery\n *\/\n$compileProvider.directive('onBlur', function() {\n\treturn {\n\t\trestrict : 'A',\n\t\tlink : function(scope, elm, attrs) {\n\t\t\telm.bind('blur', function() {\n\t\t\t\tscope.$apply(attrs.onBlur);\n\t\t\t});\n\t\t}\n\t};\n});<\/pre>\n<p>\u8fd9\u5df2\u7ecf\u5f88\u597d\u4e86\u3002<\/p>\n<p>\u4f46\u662f\u8fd8\u4e0d\u591f\u5b8c\u7f8e\u3002\u7531\u4e8e$apply\u65b9\u6cd5\u63a5\u53d7\u51fd\u6570\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u76f4\u63a5\u50cf\u4e0a\u9762\u8fd9\u6837\u5199\uff0c\u6709\u53ef\u80fd\u5bfc\u81f4angularjs\u8fd0\u884c\u65f6\u62a5\u9519\uff1a$apply already in progress<\/p>\n<p>\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u7684\u53d1\u751f\uff0c\u5219\u9700\u8981\u5bf9$apply\u65b9\u6cd5\u8fdb\u884c\u52a0\u5de5\uff1a<\/p>\n<pre>\/* factory function safeApply\n *\n * @description If you find yourself triggering the '$apply already in progress' error while developing with Angular.JS\n * (for me I find I hit most often when integrating third party plugins that trigger a lot of DOM events),\n * you can use a 'safeApply' method that checks the current phase before executing your function.\n *\n * @param scope, the action scope, mostly is the topmost controller\n * @param fn, the function which you want to apply into scope\n * @see  https:\/\/coderwall.com\/p\/ngisma\n *\/.factory('safeApply', function($rootScope) {\n\treturn function(scope, fn) {\n\t\tvar phase = scope.$root.$$phase;\n\t\tif (phase == '$apply' || phase == '$digest') {\n\t\t\tif (fn &amp;&amp; ( typeof (fn) === 'function')) {\n\t\t\t\tfn();\n\t\t\t}\n\t\t} else {\n\t\t\tscope.$apply(fn);\n\t\t}\n\t}\n});<\/pre>\n<p>\u90a3\u4e48\u4e4b\u524d\u7684onblur\u6807\u7b7e\uff0c\u5c31\u5e94\u8be5\u6539\u4e3a\uff1a<\/p>\n<pre>\/*\n * angular directive onBlur\n *\n * @description my ng-blur\n * @require jquery\n *\/\n$compileProvider.directive('onBlur', function(safeApply) {\n\treturn {\n\t\trestrict : 'A',\n\t\tlink : function(scope, elm, attrs) {\n\t\t\telm.bind('blur', function() {\n\t\t\t\tsafeApply(scope, attrs.onBlur);\n\t\t\t});\n\t\t}\n\t};\n});<\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6211\u5df2\u7ecf\u52a0\u5165\u4e86\u81ea\u5df1\u7684angular_extend\u6a21\u5757\uff0c\u5728\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u4e86\uff0c\u6548\u679c\u5f88\u597d\u3002<\/p>\n<p>&nbsp;<\/p>\n<h4>\u5c06jquery \u63d2\u4ef6\u7528angularjs\u7684\u65b9\u5f0f\u5c01\u88c5\u6210\u7ec4\u4ef6\u7684\u4f8b\u5b50<\/h4>\n<p>icheck\u662f\u4e00\u4e2ajquery\u63d2\u4ef6\uff0c\u7528\u4e8e\u8de8\u6d4f\u89c8\u5668\u7f8e\u5316Checkbox\u548cRadio\u6309\u7ebd\u3002\u5173\u4e8e\u5b83\u7684\u4ecb\u7ecd\uff0c\u5728<a href=\"http:\/\/www.bootcss.com\/p\/icheck\/\">http:\/\/www.bootcss.com\/p\/icheck\/<\/a><\/p>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u662f\u5728dom\u8f7d\u5165\u540e\u52a0\u4e00\u6bb5jquery\u4ee3\u7801\uff1a<\/p>\n<pre>$('input').iCheck({\n\tlabelHover : false,\n\tcursor : true,\n\tcheckboxClass : 'icheckbox_square-blue',\n\tradioClass : 'iradio_square-blue',\n\tincreaseArea : '20%'\n});<\/pre>\n<p>\u4f46\u662f\u65e2\u7136\u8981\u653e\u5728\u6211\u4eec\u7684\u9879\u76ee\u91cc\uff0c\u5c31\u4e0d\u80fd\u5230\u5904\u585e\u8fd9\u79cd\u76f4\u63a5\u64cd\u4f5cdom\u7684jquery\u4ee3\u7801\uff0c\u65e2\u4e0d\u7f8e\u89c2\uff0c\u4e5f\u4e0d\u6613\u7ef4\u62a4\u3002\u6309\u7167\u4e4b\u524d\u6240\u8bf4\u7684\u539f\u5219\uff0c\u6700\u597d\u5c06\u5176\u5c01\u88c5\u6210angular\u6307\u4ee4\u7684\u6a21\u5f0f\uff0c\u653e\u5728\u516c\u5171\u6a21\u5757\u91cc\u6765\u8c03\u7528\u3002\u8fd9\u91cc\u6211\u5c06\u6211\u65b0\u5efa\u7684\u6307\u4ee4\u547d\u540d\u4e3ang-icheck\u3002\u5982\u6b64\uff0c<span style=\"font-size: 13px;line-height: 19px\">\u6211\u4eec\u53ea\u8981\u5199\u5728\u67d0\u4e2acheckbox\u6216\u8005radio\u7684html\u6807\u7b7e\u91cc\u52a0\u4e0a\u4e00\u53e5ng-ickeck\u5373\u53ef\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/span><\/p>\n<pre>\/*\n * angular directive ng-icheck\n *\n * @description icheck is a plugin of jquery for beautifying checkbox &amp; radio, now I complied it with angular directive\n * @require jquery, icheck\n * @example &lt;input type=\"radio\" ng-model=\"paomian\" value=\"kangshifu\" ng-icheck&gt;\n *          &lt;input type=\"checkbox\" class=\"icheckbox\" name=\"mantou\" ng-model=\"mantou\" ng-icheck checked&gt;\n *\/\n$compileProvider.directive('ngIcheck', function($compile) {\n\treturn {\n\t\trestrict : 'A',\n\t\trequire : '?ngModel',\n\t\tlink : function($scope, $element, $attrs, $ngModel) {\n\t\t\tif (!$ngModel) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t\/\/using iCheck\n\t\t\t$($element).iCheck({\n\t\t\t\tlabelHover : false,\n\t\t\t\tcursor : true,\n\t\t\t\tcheckboxClass : 'icheckbox_square-blue',\n\t\t\t\tradioClass : 'iradio_square-blue',\n\t\t\t\tincreaseArea : '20%'\n\t\t\t}).on('ifClicked', function(event) {\n\t\t\t\tif ($attrs.type == \"checkbox\") {\n\t\t\t\t\t\/\/checkbox, $ViewValue = true\/false\/undefined\n\t\t\t\t\t$scope.$apply(function() {\n\t\t\t\t\t\t$ngModel.$setViewValue(!($ngModel.$modelValue == undefined ? false : $ngModel.$modelValue));\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\t\/\/ radio, $ViewValue = $attrs.value\n\t\t\t\t\t$scope.$apply(function() {\n\t\t\t\t\t\t$ngModel.$setViewValue($attrs.value);\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n});<\/pre>\n<p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff1a\u4f7f\u7528\u4e86icheck\u63d2\u4ef6\u540e\uff0c\u4f1a\u751f\u6210\u4e00\u4e2a\u7f8e\u5316\u8fc7\u7684div\u8986\u76d6\u5728\u539f\u6765\u7684checkbox\u6216\u8005radio\u4e4b\u4e0a\uff0c\u800c\u539f\u6765\u7684checkbox\u6216\u8005radio\u4f1a\u88ab\u5f71\u85cf\u3002\u6545\u800c\uff0c\u5f53\u6211\u4eec\u70b9\u51fb\u5b83\u4eec\u65f6\uff0c\u4e0d\u4f1a\u76f4\u63a5\u89e6\u53d1\u4e8b\u4ef6\uff0c\u4f7f\u5f97\u7ed1\u5b9a\u5230checkbox\u6216\u8005radio\u4e0a\u7684model\u503c\u6539\u53d8\u3002\u6240\u4ee5\u6211\u4eec\u8fd9\u91cc\u9700\u8981\u91cd\u65b0\u7ed1\u5b9a\u4e8b\u4ef6\uff0c\u4f7f\u7528<\/p>\n<pre>$ngModel.$setViewValue()<\/pre>\n<p>\u65b9\u6cd5\u6765\u7ed9model\u8d4b\u503c\u3002\u5177\u4f53\u903b\u8f91\uff0c\u5219\u76f8\u6839\u636echeckbox\u548cradio\u800c\u4e0d\u540c\u3002\u8be6\u89c1\u4ee5\u4e0a\u4ee3\u7801\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u7531\u4e8e\u4ee5\u4e0a\u4ee3\u7801\u5199\u5728\u6211\u7684\u9879\u76ee\u4e2d\u7684\u901a\u7528\u6a21\u5757common_angular_component.js\u91cc\uff0c\u6545\u800c\u5728\u8c03\u7528\u4e86\u8be5\u901a\u7528\u6a21\u5757\u7684\u9875\u9762\u91cc\uff0c\u76f4\u63a5\u4f7f\u7528ng-icheck\u6307\u4ee4\u5373\u53ef\u5b9e\u73b0ickeck\u7684\u7f8e\u5316\u6548\u679c\uff0c\u540c\u65f6\u907f\u514d\u4e86\u5927\u91cf\u91cd\u590d\u7684jquery\u4ee3\u7801\u7684\u51fa\u73b0\u3002<\/p>\n<pre>&lt;input type=\"radio\" ng-model=\"paomian\" value=\"kangshifu\" ng-icheck&gt;\n&lt;input type=\"checkbox\" name=\"mantou\" ng-model=\"mantou\" ng-icheck checked&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00\uff1aangularjs\u662f\u4e00\u4e2a\u4e2d\u7b49\u91cd\u91cf\u7ea7<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[16,37,78,79,163],"class_list":["post-485","post","type-post","status-publish","format-standard","hentry","category-web-frontend","tag-angularjs","tag-javascript","tag-78","tag-79","tag-163"],"_links":{"self":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/posts\/485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/comments?post=485"}],"version-history":[{"count":0,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/posts\/485\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/media?parent=485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/categories?post=485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/tags?post=485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}