{"id":769,"date":"2017-03-07T22:25:06","date_gmt":"2017-03-07T14:25:06","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=769"},"modified":"2017-03-07T22:26:51","modified_gmt":"2017-03-07T14:26:51","slug":"angular%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=769","title":{"rendered":"angular\u5e38\u89c1\u95ee\u9898"},"content":{"rendered":"<p>AngularJS\u53ef\u4ee5\u89c6\u4e3a\u662f\u4e00\u79cd\u6570\u636e\u4f18\u5148\u7684\u6846\u67b6\uff0c\u5728\u5b83\u7684\u4e09\u4e2a\u5c42\u9762\u4e2d\uff0c\u6570\u636e\u6a21\u578b\u662f\u9aa8\u67b6\uff0c\u89c6\u56fe\u6a21\u578b\u548c\u4e1a\u52a1\u4e8b\u4ef6\u662f\u8840\u8089\uff0c\u89c6\u56fe\u6a21\u677f\u548c\u6307\u4ee4\u662f\u76ae\u6bdb\uff0c\u8fd9\u4e09\u5c42\u5408\u5728\u4e00\u8d77\uff0c\u5c31\u5f62\u6210\u4e86\u4e00\u4e2a\u6d3b\u751f\u751f\u7684Web\u5e94\u7528\u3002<br \/>\n1.ng-if\u7684\u60c5\u51b5\u4e0b \uff0c\u59cb\u7ec8\u5c06\u9875\u9762\u4e2d\u7684\u5143\u7d20\u7ed1\u5b9a\u5230\u5bf9\u8c61\u7684\u5c5e\u6027\uff08data.x\uff09\u800c\u4e0d\u662f\u76f4\u63a5\u7ed1\u5b9a\u5230\u57fa\u672c\u53d8\u91cf\uff08x\uff09\u4e0a\u3002\u56e0\u4e3ang-if \u4f1a\uff08\u9690\u5f0f\u5730\uff09\u4ea7\u751f\u65b0\u4f5c\u7528\u57df\u3002<br \/>\n2.ng-repeat\u8fed\u4ee3\u6570\u7ec4\u7684\u65f6\u5019\uff0c\u5982\u679c\u6570\u7ec4\u4e2d\u6709\u76f8\u540c\u503c\uff0c\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u5982\u4f55\u89e3\u51b3?\u00a0\u00a0\u52a0 track by $index \u53ef\u89e3\u51b3\u3002\u4e5f\u53ef\u4ee5 trace by \u4efb\u4f55\u4e00\u4e2a\u666e\u901a\u7684\u503c<br \/>\n3.ng-click \u4e2d\u5199\u7684\u8868\u8fbe\u5f0f\uff0c\u80fd\u4f7f\u7528 JS \u539f\u751f\u5bf9\u8c61\u4e0a\u7684\u65b9\u6cd5\u5417?\u4e0d\u80fd\u7528\uff0c\u53ea\u8981\u662f\u5728\u9875\u9762\u4e2d\uff0c\u90fd\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u539f\u751f\u7684 JS \u65b9\u6cd5\u3002\u56e0\u4e3a\u8fd9\u4e9b\u5e76\u4e0d\u5b58\u5728\u4e8e\u4e0e\u9875\u9762\u5bf9\u5e94\u7684 Controller \u7684 $scope \u4e2d\u3002<\/p>\n<pre class=\"brush:html;toolbar:false\">&lt;p&gt;{{13.14 | parseIntFilter}}&lt;\/p&gt;\r\napp.filter('parseIntFilter', function(){\r\n    return function(item){\r\n        return parseInt(item);\r\n    }\r\n})<\/pre>\n<p>4.{{now | &#8216;yyyy-MM-dd&#8217;}} \u8fd9\u79cd\u8868\u8fbe\u5f0f\u91cc\u9762\uff0c\u7ad6\u7ebf\u548c\u540e\u9762\u7684\u53c2\u6570\u901a\u8fc7\u4ec0\u4e48\u65b9\u5f0f\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff1f<br \/>\nng \u5185\u7f6e\u7684 filter \u6709\u4e5d\u79cd\uff1a<\/p>\n<p>date\uff08\u65e5\u671f\uff09<br \/>\ncurrency\uff08\u8d27\u5e01\uff09<br \/>\nlimitTo\uff08\u9650\u5236\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u957f\u5ea6\uff09<br \/>\norderBy\uff08\u6392\u5e8f\uff09<br \/>\nlowercase\uff08\u5c0f\u5199\uff09<br \/>\nuppercase\uff08\u5927\u5199\uff09<br \/>\nnumber\uff08\u683c\u5f0f\u5316\u6570\u5b57\uff0c\u52a0\u4e0a\u5343\u4f4d\u5206\u9694\u7b26\uff0c\u5e76\u63a5\u6536\u53c2\u6570\u9650\u5b9a\u5c0f\u6570\u70b9\u4f4d\u6570\uff09<br \/>\nfilter\uff08\u5904\u7406\u4e00\u4e2a\u6570\u7ec4\uff0c\u8fc7\u6ee4\u51fa\u542b\u6709\u67d0\u4e2a\u5b50\u4e32\u7684\u5143\u7d20\uff09<br \/>\njson\uff08\u683c\u5f0f\u5316 json \u5bf9\u8c61\uff09<br \/>\n5.filter \u6709\u4e24\u79cd\u4f7f\u7528\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u76f4\u63a5\u5728\u9875\u9762\u91cc\uff1a<\/p>\n<p>&lt;p&gt;{{now | date : &#8216;yyyy-MM-dd&#8217;}}&lt;\/p&gt;<\/p>\n<p>\u53e6\u4e00\u79cd\u662f\u5728 js \u91cc\u9762\u7528\uff1a<\/p>\n<p>\/\/ $filter(&#8216;\u8fc7\u6ee4\u5668\u540d\u79f0&#8217;)(\u9700\u8981\u8fc7\u6ee4\u7684\u5bf9\u8c61, \u53c2\u65701, \u53c2\u65702,&#8230;)<br \/>\n$filter(&#8216;date&#8217;)(now, &#8216;yyyy-MM-dd hh:mm:ss&#8217;);<\/p>\n<p>\u81ea\u5b9a\u4e49 filter<\/p>\n<pre class=\"brush:html;toolbar:false\">\/\/ \u5f62\u5f0f\r\napp.filter('\u8fc7\u6ee4\u5668\u540d\u79f0',function(){\r\n    return function(\u9700\u8981\u8fc7\u6ee4\u7684\u5bf9\u8c61,\u8fc7\u6ee4\u5668\u53c2\u65701,\u8fc7\u6ee4\u5668\u53c2\u65702,...){\r\n        \/\/...\u505a\u4e00\u4e9b\u4e8b\u60c5  \r\n        return \u5904\u7406\u540e\u7684\u5bf9\u8c61;\r\n    }\r\n});<\/pre>\n<pre class=\"brush:html;toolbar:false\">\/\/ \u4f8b\u5b50\r\napp.filter('timesFilter', function(){\r\n    return function(item, times){\r\n        var result = '';\r\n        for(var i = 0; i &lt; times; i++){\r\n            result += item;\r\n        }\r\n        return result;\r\n    }\r\n})<\/pre>\n<p>6.factory\u3001service \u548c provider \u662f\u4ec0\u4e48\u5173\u7cfb\uff1f<br \/>\nfactory\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800cservice\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0c\u7ed1\u5b9a\u5230 this \u7684\u90fd\u53ef\u4ee5\u88ab\u8bbf\u95ee\u3002provider \u662f\u52a0\u5f3a\u7248 factory\uff0c\u8fd4\u56de\u4e00\u4e2a\u53ef\u914d\u7f6e\u7684 factory<br \/>\n7.\u6027\u80fd\u95ee\u9898<\/p>\n<p>\u4f5c\u4e3a MVVM \u6846\u67b6\uff0c\u56e0\u4e3a\u5b9e\u73b0\u4e86\u6570\u636e\u7684\u53cc\u5411\u7ed1\u5b9a\uff0c\u5bf9\u4e8e\u5927\u6570\u7ec4\u3001\u590d\u6742\u5bf9\u8c61\u4f1a\u5b58\u5728\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<p>\u53ef\u4ee5\u7528\u6765 \u4f18\u5316 Angular \u5e94\u7528\u7684\u6027\u80fd \u7684\u529e\u6cd5\uff1a<\/p>\n<p>\u51cf\u5c11\u76d1\u63a7\u9879\uff08\u6bd4\u5982\u5bf9\u4e0d\u4f1a\u53d8\u5316\u7684\u6570\u636e\u91c7\u7528\u5355\u5411\u7ed1\u5b9a\uff09<br \/>\n\u4e3b\u52a8\u8bbe\u7f6e\u7d22\u5f15\uff08\u6307\u5b9a track by\uff0c\u7b80\u5355\u7c7b\u578b\u9ed8\u8ba4\u7528\u81ea\u8eab\u5f53\u7d22\u5f15\uff0c\u5bf9\u8c61\u9ed8\u8ba4\u4f7f\u7528 $$hashKey\uff0c\u6bd4\u5982\u6539\u4e3a track by item.id\uff09<br \/>\n\u964d\u4f4e\u6e32\u67d3\u6570\u636e\u91cf\uff08\u6bd4\u5982\u5206\u9875\uff0c\u6216\u8005\u6bcf\u6b21\u53d6\u4e00\u5c0f\u90e8\u5206\u6570\u636e\uff0c\u6839\u636e\u9700\u8981\u518d\u53d6\uff09<br \/>\n\u6570\u636e\u6241\u5e73\u5316\uff08\u6bd4\u5982\u5bf9\u4e8e\u6811\u72b6\u7ed3\u6784\uff0c\u4f7f\u7528\u6241\u5e73\u5316\u7ed3\u6784\uff0c\u6784\u5efa\u4e00\u4e2a map \u548c\u6811\u72b6\u6570\u636e\uff0c\u5bf9\u6811\u64cd\u4f5c\u65f6\uff0c\u7531\u4e8e\u8ddf\u6241\u5e73\u6570\u636e\u540c\u4e00\u5f15\u7528\uff0c\u6811\u72b6\u6570\u636e\u53d8\u66f4\u4f1a\u540c\u6b65\u5230\u539f\u59cb\u7684\u6241\u5e73\u6570\u636e\uff09<\/p>\n<p class=\"post-copyright\">\u6b22\u8fce\u5206\u4eab\u672c\u6587\uff0c\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u51fa\u5904\uff1a<a href=\"http:\/\/www.qdabc.cn\">\u524d\u7aefABC<\/a> &raquo; <a href=\"http:\/\/www.qdabc.cn\/?p=769\">angular\u5e38\u89c1\u95ee\u9898<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>AngularJS\u53ef\u4ee5\u89c6\u4e3a\u662f\u4e00\u79cd\u6570\u636e\u4f18\u5148\u7684\u6846\u67b6\uff0c\u5728\u5b83\u7684\u4e09\u4e2a\u5c42\u9762\u4e2d\uff0c\u6570\u636e\u6a21\u578b\u662f\u9aa8\u67b6\uff0c\u89c6\u56fe\u6a21\u578b\u548c\u4e1a\u52a1\u4e8b\u4ef6\u662f\u8840\u8089\uff0c\u89c6\u56fe\u6a21\u677f\u548c\u6307\u4ee4\u662f\u76ae\u6bdb\uff0c\u8fd9\u4e09\u5c42\u5408\u5728\u4e00\u8d77\uff0c\u5c31\u5f62\u6210\u4e86\u4e00\u4e2a\u6d3b\u751f\u751f\u7684Web\u5e94\u7528\u3002 1.ng-if\u7684\u60c5\u51b5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":324,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[53],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/769"}],"collection":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=769"}],"version-history":[{"count":4,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/769\/revisions"}],"predecessor-version":[{"id":773,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/769\/revisions\/773"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/media\/324"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=769"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}