{"id":858,"date":"2016-09-22T21:15:07","date_gmt":"2016-09-22T13:15:07","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=858"},"modified":"2016-09-22T21:15:07","modified_gmt":"2016-09-22T13:15:07","slug":"angularjs%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%e5%85%ad%e7%bb%84%e4%bb%b6%e5%8c%96%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=858","title":{"rendered":"AngularJS\u5165\u95e8\u6559\u7a0b(\u516d)\u7ec4\u4ef6\u5316\u5f00\u53d1"},"content":{"rendered":"<p>\n\t\u63a5\u7740\u4e0a\u4e00\u7ae0AngularJS\u7684\u6307\u4ee4\u5b66\u4e60\uff0c\u672c\u7ae0\u6211\u5c06\u5e26\u9886\u5927\u5bb6\u5b66\u4e60AngularJS\u7684\u7ec4\u4ef6\u3002\u5305\u542b\u53ef\u590d\u7528\u7ec4\u4ef6\u7684\u521b\u5efa\u3001\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f\u4ee5\u53ca\u4e00\u4e9b\u81ea\u5e26\u7ec4\u4ef6\u7684\u4f7f\u7528\u3002\n<\/p>\n<h2>\n\tAngularJS\u7ec4\u4ef6\uff08Component\uff09<br \/>\n<\/h2>\n<p>\n\t\u7ec4\u4ef6\u5176\u5b9e\u5c31\u662f\u6307\u4ee4\u7684\u4e00\u79cd\u7279\u6027\u5f62\u5f0f\uff0c\u5b83\u89c4\u907f\u4e86\u4e00\u4e9b\u6307\u4ee4\u4e2d\u6666\u6da9\u96be\u7406\u89e3\u7684\u4e1c\u897f\uff0c\u6bd4\u5982compile\u51fd\u6570\uff0clink\u51fd\u6570\uff0cscope\uff0crestrict\u7b49\uff0c\u6240\u4ee5\u7ec4\u4ef6\u7684\u76ee\u7684\u5c31\u662f\u80fd\u8ba9\u6211\u4eec\u66f4\u4e3a\u50bb\u74dc\u5f0f\u7684\u521b\u5efa\u6307\u4ee4\uff0c\u80fd\u66f4\u597d\u7684\u9075\u5faa\u7ec4\u4ef6\u5316\u7684\u5f00\u53d1\u6a21\u5f0f\uff0c\u63d0\u9ad8\u6027\u80fd\u4ee5\u53ca\u66f4\u5bb9\u6613\u5411AngularJS 2.0\u8fc1\u79fb\u3002\n<\/p>\n<h2>\n\t\u7ec4\u4ef6\u7684\u521b\u5efa<br \/>\n<\/h2>\n<p>\n\t\u4f7f\u7528Module\u7684component\u65b9\u6cd5\u521b\u5efa\u7ec4\u4ef6\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar mainModule = angular.module(&quot;mainModule&quot;, []);\r\nmainModule.component(&quot;myComponent&quot;, {\r\n  templateUrl: &quot;myTemplate.html&quot;,\r\n  controller: function() {\r\n\r\n  },\r\n  bindings: {\r\n    name: &quot;=&quot;\r\n  }\r\n});<\/pre>\n<p>\n\tcomponent\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7ec4\u4ef6\u540d\u79f0\uff0c\u547d\u540d\u89c4\u5219\u548c\u4f7f\u7528\u65b9\u6cd5\u4e0e\u6307\u4ee4\u4e00\u6837\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u548c\u521b\u5efa\u6307\u4ee4\u6709\u70b9\u4e0d\u540c\uff0c\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5728\u8be5\u5bf9\u8c61\u4e2d\u5bf9\u7ec4\u4ef6\u7684\u914d\u7f6e\u548c\u5728\u6307\u4ee4\u4e2d\u7684\u914d\u7f6e\u65b9\u5f0f\u5f88\u7c7b\u4f3c\u3002\n<\/p>\n<p>\n\t\u6211\u4eec\u5148\u6765\u770b\u770b\u7ec4\u4ef6\u548c\u6307\u4ee4\u4e4b\u95f4\u6709\u54ea\u4e9b\u533a\u522b\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\u7ec4\u4ef6\u4e2d\u4e0d\u63d0\u4f9b\u624b\u52a8\u914d\u7f6e\u4f5c\u7528\u57df\uff0c\u9ed8\u8ba4\u7684\u4f5c\u7528\u57df\u5c31\u662f\u9694\u79bb\u57df\u3002&nbsp;\n\t<\/li>\n<li>\n\t\t\u7ec4\u4ef6\u4e2d\u901a\u8fc7bindings\u5c5e\u6027\u8fdb\u884c\u6570\u636e\u7ed1\u5b9a\uff0c\u9664\u4e86=\uff0c@\uff0c&amp;\u4e09\u79cd\u7ed1\u5b9a\u65b9\u5f0f\u4ee5\u5916\u8fd8\u589e\u52a0\u4e86\u4e00\u79cd&lt;\u65b9\u5f0f\uff0c\u65e2\u5355\u5411\u7ed1\u5b9a\uff0c\u4f46\u4e0d\u9650\u4e8e\u5b57\u7b26\u4e32\u3002\u4ece\u800c\u4fdd\u8bc1\u4e86\u7ec4\u4ef6\u6709\u81ea\u5df1\u7684\u6e05\u6670\u7684\u8f93\u5165\u8f93\u51faAPI\u3002\u5e76\u4e14\u901a\u8fc7bindings\u5bf9\u8c61\u7ed1\u5b9a\u7684\u5c5e\u6027\u76f4\u63a5\u7ed1\u5b9a\u5728\u7ec4\u4ef6\u7684Controller\u4e0a\u3002&nbsp;\n\t<\/li>\n<li>\n\t\t\u7ec4\u4ef6\u7684Controller\u9ed8\u8ba4\u540d\u79f0\u4e3a$ctrl\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4f7f\u7528controllerAs\u5c5e\u6027\u6307\u5b9aController\u7684\u540d\u79f0\u3002&nbsp;\n\t<\/li>\n<li>\n\t\t\u7ec4\u4ef6\u53ea\u80fd\u4ee5\u6807\u7b7e\u5f62\u5f0f\u4f7f\u7528\u3002&nbsp;\n\t<\/li>\n<li>\n\t\t\u7ec4\u4ef6\u4e2d\u6ca1\u6709link\u51fd\u6570\uff0ccompile\u51fd\u6570\uff0cpriority\u5c5e\u6027\uff0crestrict\u5c5e\u6027\u3002&nbsp;\n\t<\/li>\n<li>\n\t\t\u7ec4\u4ef6\u53ea\u80fd\u63a7\u5236\u81ea\u8eab\u7684\u8f93\u5165\u8f93\u51fa\uff0c\u7ec4\u4ef6\u4e0d\u5141\u8bb8\u4fee\u6539\u5c5e\u4e8e\u81ea\u5df1\u9694\u79bb\u57df\u4ee5\u5916\u7684\u4efb\u4f55\u6570\u636e\u548cDOM\u5143\u7d20\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cAngularJS\u901a\u8fc7\u4f5c\u7528\u57df\uff08Scope\uff09\u7ee7\u627f\u7684\u7279\u6027\u652f\u6301\u8de8\u5c42\u7ea7\u4fee\u6539\u6570\u636e\u7684\u80fd\u529b\uff0c\u4f46\u662f\u5982\u679c\u5f53\u4fee\u6539\u6570\u636e\u804c\u8d23\u4e0d\u6e05\u6670\u6216\u4e0d\u6070\u5f53\u7684\u65f6\u5019\u5c31\u4f1a\u5bfc\u81f4\u5404\u79cd\u95ee\u9898\uff0c\u6240\u4ee5\u8fd9\u4e5f\u5c31\u662f\u7ec4\u4ef6\u7684\u4f5c\u7528\u57df\u9ed8\u8ba4\u90fd\u662f\u9694\u79bb\u57df\u7684\u539f\u56e0\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u4f7f\u7528\u8d77\u6765\u548c\u6307\u4ee4\u6bd4\u8f83\u7c7b\u4f3c\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\n\/\/ modules.js\r\nvar mainModule = angular.module(&quot;mainModule&quot;, []);\r\nmainModule.controller(&quot;MyController&quot;, function() {\r\n  this.person = {\r\n    name: &quot;Jason&quot;\r\n  }\r\n})\r\nmainModule.component(&quot;myComponent&quot;, {\r\n  templateUrl: &quot;myTemplate.html&quot;,\r\n  controller: function() {\r\n\r\n  },\r\n  bindings: {\r\n    person: &quot;=&quot;\r\n  }\r\n});<\/pre>\n<pre class=\"brush:xml;\">\r\n&lt;!--index.html--&gt;\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;meta charset=&quot;utf-8&quot;&gt;\r\n    &lt;title&gt;Demo for Component&lt;\/title&gt;\r\n    &lt;script src=&quot;..\/angular-1.5.8.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;modules.js&quot;&gt;&lt;\/script&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body ng-app=&quot;mainModule&quot;&gt;\r\n    &lt;div ng-controller=&quot;MyController as mc&quot;&gt;\r\n      &lt;my-component person=&quot;mc.person&quot;&gt;&lt;\/my-component&gt;\r\n    &lt;\/div&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<pre class=\"brush:xml;\">\r\n&lt;!--myTemplate.html--&gt;\r\n&lt;span&gt;Name: {{$ctrl.person.name}}&lt;\/span&gt;<\/pre>\n<h2>\n\t\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f<br \/>\n<\/h2>\n<p>\n\t\u5728\u7ec4\u4ef6\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u91cc\uff0cAngularJS\u63d0\u4f9b\u4e86\u4e94\u4e2a\u5173\u952e\u70b9\u7684\u65b9\u6cd5\uff0c\u53ef\u4f9b\u6211\u4eec\u76d1\u542c\u5230\u7ec4\u4ef6\u7684\u8fd0\u884c\u72b6\u6001\uff1a&nbsp;\n<\/p>\n<ul>\n<li>\n\t\t$onInit()\uff1a\u8be5\u65b9\u6cd5\u5728\u7ec4\u4ef6\u53ca\u5176\u6240\u6709 binding \u521d\u59cb\u5316\u4e4b\u540e\u88ab\u8c03\u7528\uff0c\u4ece\u800c\u6211\u4eec\u5c31\u6709\u4e86\u4e00\u4e2a\u6e05\u6670\u7684\u5730\u65b9\u7edf\u4e00\u5b58\u653e\u6570\u636e\u521d\u59cb\u5316\u7684\u903b\u8f91\uff1a<\/p>\n<pre class=\"brush:xml;\">\r\nvar mainModule = angular.module(&quot;mainModule&quot;, []);\r\nmainModule.component(&quot;myComponent&quot;, {\r\n  templateUrl: &quot;myTemplate.html&quot;,\r\n  controller: function() {\r\n    this.name = &quot;jason&quot;;\r\n  }\r\n});\r\n\/\/ \u5c06\u521d\u59cb\u5316\u6570\u636e\u7684\u903b\u8f91\u653e\u5728onInit\u65b9\u6cd5\u4e2d\r\nmainModule.component(&quot;myComponent&quot;, {\r\n  templateUrl: &quot;myTemplate.html&quot;,\r\n  controller: function() {\r\n    this.$onInit = function() {\r\n      this.name = &quot;jason&quot;;\r\n    }\r\n  }\r\n});\r\n<\/pre>\n<\/li>\n<li>\n\t\t$onChanges(changesObj)\uff1a\u5f53\u7ec4\u4ef6\u4e2d\u5355\u5411\u7ed1\u5b9a\u7684\u5c5e\u6027\u503c\u53d1\u751f\u53d8\u5316\u65f6\u88ab\u8c03\u7528\uff0c\u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f\u53ea\u6709\u7ed1\u5b9a\u5c5e\u6027\u503c\u7684\u5f15\u7528\u53d1\u751f\u53d8\u5316\u65f6\u624d\u80fd\u76d1\u542c\u5230\uff0c\u5982\u679c\u53ea\u662f\u5728\u6307\u4ee4\u5185\u5bf9\u5c5e\u6027\u8fdb\u884c\u4fee\u6539\uff0c\u8be5\u65b9\u6cd5\u662f\u65e0\u6cd5\u76d1\u542c\u5230\u7684\u3002\u901a\u8fc7\u8be5\u65b9\u6cd5\u7684\u53c2\u6570\u53ef\u4ee5\u83b7\u53d6\u5230\u88ab\u4fee\u6539\u6570\u636e\u5f53\u524d\u7684\u503c\u3001\u4fee\u6539\u4e4b\u524d\u7684\u503c\u3001\u662f\u5426\u65f6\u7b2c\u4e00\u6b21\u4fee\u6539\uff1a<\/p>\n<pre class=\"brush:jscript;\">\r\nmainModule.component(&quot;myComponent&quot;, {\r\n  templateUrl: &quot;myTemplate.html&quot;,\r\n  controller: function() {\r\n    this.$onChanges = function(changesObj) {\r\n      if(changesObj.name) {\r\n        \/\/ name\u5f53\u524d\u7684\u503c\r\n        var nameCurrentValue = changesObj.name.currentValue;\r\n        \/\/ name\u4fee\u6539\u524d\u7684\u503c\r\n        var namePreviousValue = changesObj.name.previousValue;\r\n        \/\/ \u662f\u5426\u662f\u7b2c\u4e00\u6b21\u4fee\u6539\r\n        var isFirstChange = changesObj.name.isFirstChange();\r\n      }\r\n    }\r\n  },\r\n  bindings: {\r\n    name: &quot;&lt;&quot;\r\n  }\r\n});<\/pre>\n<\/li>\n<li>\n\t\t$doCheck()\uff1a\u8be5\u65b9\u6cd5\u548c$onChanges(changesObj)\u4f5c\u7528\u7c7b\u4f3c\uff0c\u4f46\u662f\u8be5\u65b9\u6cd5\u53ef\u4ee5\u76d1\u542c\u5230\u5728\u6307\u4ee4\u5185\u5bf9\u5c5e\u6027\u8fdb\u884c\u4fee\u6539\u7684\u884c\u4e3a\uff1a<\/p>\n<pre class=\"brush:jscript;\">\r\nmainModule.component(&quot;myComponent&quot;, {\r\n  templateUrl: &quot;myTemplate.html&quot;,\r\n  controller: function() {\r\n    \/\/ \u5f53name\u5728\u6307\u4ee4\u5185\u4fee\u6539\u65f6\r\n    this.name = &quot;Green&quot;;\r\n    this.$doCheck = function() {\r\n      \/\/ doCheck\u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\r\n    }\r\n  },\r\n  bindings: {\r\n    name: &quot;&lt;&quot;\r\n  }\r\n});<\/pre>\n<\/li>\n<li>\n\t\t$onDestroy()\uff1a\u5f53\u4f5c\u7528\u57df\u88ab\u9500\u6bc1\u65f6\u8c03\u7528\u8be5\u65b9\u6cd5\u3002\n\t<\/li>\n<li>\n\t\t$postLink()\uff1a\u5f53\u6307\u4ee4\u6240\u5728\u6807\u7b7e\u4e0e\u5b50\u6807\u7b7e\u94fe\u63a5\u65f6\u8c03\u7528\u8be5\u65b9\u6cd5\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\t\u7ec4\u4ef6\u5316\u5f00\u53d1<br \/>\n<\/h2>\n<p>\n\t\u6211\u4eec\u5148\u6765\u770b\u770b\u793a\u4f8b\u6548\u679c\uff1a<br \/>\n\t<img decoding=\"async\" alt=\"AngularJS\u7ec4\u4ef6\u5f00\u53d1\u5b9e\u4f8b\" src=\"http:\/\/7xpp8a.com1.z0.glb.clouddn.com\/angularjs\/angularjs-guide-6.gif\" \/>\n<\/p>\n<p>\n\t\u65e2\u7136\u662f\u7ec4\u4ef6\u5316\u5f00\u53d1\uff0c\u90a3\u4e48\u6211\u4eec\u6765\u770b\u770b\u4e0a\u9762\u8fd9\u4e2a\u793a\u4f8b\u6709\u51e0\u4e2a\u7ec4\u4ef6\uff1a<br \/>\n\t<img decoding=\"async\" src=\"http:\/\/7xpp8a.com1.z0.glb.clouddn.com\/angularjs\/angularjs-guide-7.png\" \/>\n<\/p>\n<p>\n\t\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u5230\uff0c\u6574\u4e2a\u793a\u4f8b\u4e00\u5171\u7528\u4e86\u4e09\u4e2a\u7ec4\u4ef6\uff0c\u5176\u4e2d\u6709\u4e24\u4e2a\u7ec4\u4ef6\u8fdb\u884c\u4e86\u590d\u7528\uff0c\u4e0b\u9762\u6211\u4eec\u6765\u770b\u770b\u6bcf\u4e2a\u7ec4\u4ef6\u662f\u5982\u4f55\u5b9a\u4e49\u7684\u3002\n<\/p>\n<h2>\n\t\u81ea\u5b9a\u4e49personList\u7ec4\u4ef6<br \/>\n<\/h2>\n<p>\n\t\u8be5\u7ec4\u4ef6\u4e3b\u8981\u7528\u6765\u521d\u59cb\u5316\u6570\u636e\u6e90\uff0c\u5b9a\u4e49\u5bf9\u6570\u636e\u6e90\u64cd\u4f5c\u7684\u51fd\u6570\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nmainModule.component(&quot;personList&quot;, {\r\n  templateUrl: &quot;personList.html&quot;,\r\n  controller: function() {\r\n    this.$onInit = function() {\r\n      this.list = [{\r\n        name: &quot;Jason&quot;,\r\n        job: &quot;Developer&quot;\r\n      },{\r\n        name: &quot;Green&quot;,\r\n        job: &quot;Doctor&quot;\r\n      }];\r\n    };\r\n    this.updatePerson = function(person, job, value) {\r\n      person[job] = value;\r\n    };\r\n    this.deletePerson = function(person) {\r\n      var idx = this.list.indexOf(person);\r\n      if(idx &gt;= 0) {\r\n        this.list.splice(idx, 1);\r\n      }\r\n    };\r\n  }\r\n});<\/pre>\n<p>\n\t\u9996\u5148\u5728$onInit\u51fd\u6570\u4e2d\u521d\u59cb\u5316\u6570\u636e\u6e90\uff0c\u5b9a\u4e49\u4e86Person\u5bf9\u8c61\u6570\u7ec4\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u66f4\u65b0\u6307\u5b9aPerson\u5bf9\u8c61\u7684\u65b9\u6cd5updatePerson\u53ca\u5220\u9664\u6307\u5b9aPerson\u5bf9\u8c61\u7684\u65b9\u6cd5deletePerson\u3002\n<\/p>\n<p>\n\t\u518d\u6765\u770b\u770b\u5b83\u7684UI\u6a21\u677f\u6587\u4ef6personList.html\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;b&gt;Person&lt;\/b&gt;&lt;br&gt;\r\n&lt;person-detail ng-repeat=&quot;person in $ctrl.list&quot; person=&quot;person&quot; on-update=&quot;$ctrl.updatePerson(person, job, value)&quot; on-delete=&quot;$ctrl.deletePerson(person)&quot;&gt;&lt;\/person-detail&gt;<\/pre>\n<p>\n\t\u8be5\u6587\u4ef6\u5171\u6709\u4e24\u90e8\u5206\uff0c\u7b2c\u4e00\u90e8\u5206\u662f\u7528\u539f\u751fHTML\u6807\u7b7e\u5b9a\u4e49\u4e86\u6807\u9898\uff0c\u7b2c\u4e8c\u90e8\u5206\u662f\u4f7f\u7528\u4e86\u53e6\u5916\u4e00\u4e2a\u7ec4\u4ef6personDetail\u3002ng-repeat\u6307\u4ee4\u662fAngularJS\u5185\u7f6e\u7684\u6307\u4ee4\uff0c\u4f5c\u7528\u4e0d\u8a00\u800c\u55bb\uff0c\u5c31\u662f\u5faa\u73af\u6570\u636e\u6e90\uff0c\u540c\u65f6\u7ec4\u4ef6\u4e5f\u8ddf\u636e\u5faa\u73af\u6b21\u6570\u589e\u52a0\u3002person\uff0con-update\uff0con-delete\u662f\u5728personDetail\u7ec4\u4ef6\u4e2d\u5b9a\u4e49\u7684\u6570\u636e\u7ed1\u5b9a\u5c5e\u6027\uff0c\u7528\u5927\u767d\u8bdd\u89e3\u91ca\u5c31\u662f\uff0cpersonDetail\u7ec4\u4ef6\u4e2d\u7684person\u53d8\u91cf\u4e0epersonList\u7ec4\u4ef6\u4e2d\u7684Person\u5bf9\u8c61\u8fdb\u884c\u4e86\u7ed1\u5b9a\uff0cpersonDetail\u7ec4\u4ef6\u4e2d\u7684onUpdate\u548conDelete\u65b9\u6cd5\u5206\u522b\u4e0epersonList\u7ec4\u4ef6\u4e2d\u7684updatePerson\u548cdeletePerson\u65b9\u6cd5\u8fdb\u884c\u4e86\u7ed1\u5b9a\n<\/p>\n<h2>\n\t\u5b9a\u4e49personDetail\u7ec4\u4ef6<br \/>\n<\/h2>\n<p>\n\t\u8be5\u7ec4\u4ef6\u4e3b\u8981\u7528\u4e8e\u5c55\u793aPerson\u5bf9\u8c61\u7684\u5177\u4f53\u5185\u5bb9\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nmainModule.component(&quot;personDetail&quot;, {\r\n  templateUrl: &quot;personDetail.html&quot;,\r\n  bindings: {\r\n    person: &quot;&lt;&quot;,\r\n    onUpdate: &quot;&amp;&quot;,\r\n    onDelete: &quot;&amp;&quot;\r\n  },\r\n  controller: function() {\r\n    this.update = function(job, value) {\r\n      this.onUpdate({person: this.person, job: job, value: value});\r\n    };\r\n    this.delete = function() {\r\n      this.onDelete(this.person);\r\n    }\r\n  }\r\n});<\/pre>\n<pre class=\"brush:xml;\">\r\n&lt;hr&gt;\r\n&lt;div&gt;\r\n  Name: {{$ctrl.person.name}}&lt;br&gt;\r\n  Job: &lt;editable-field field-value=&quot;$ctrl.person.job&quot; on-update=&quot;$ctrl.update(&#039;job&#039;, value)&quot;&gt;&lt;\/editable-field&gt;\r\n  &lt;button ng-click=&quot;$ctrl.delete()&quot;&gt;Delete&lt;\/button&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>\n\t\u5728personDetail.html\u6587\u4ef6\u91cc\uff0c\u9996\u5148\u8bbf\u95ee\u4e86person\u5bf9\u8c61\u7684name\u5c5e\u6027\uff0c\u5c06\u5176\u5c55\u793a\u51fa\u6765\uff0c\u6ce8\u610f\uff0c\u8fd9\u91cc\u7531$ctrl.preson\u8bbf\u95ee\u5230\u7684\u5176\u5b9e\u662f\u5355\u5411\u7ed1\u5b9a\u7684personList\u7ec4\u4ef6\u4e2d\u7684person\u5bf9\u8c61\u3002\u800c\u4e14\u5728update\u51fd\u6570\u4e2d\u8c03\u7528\u4e86\u4e0epersonList\u7ec4\u4ef6\u7684updatePerson\u51fd\u6570\u7ed1\u5b9a\u7684onUpdate\u51fd\u6570\uff0c\u4e5f\u5c31\u662f\u5b50\u7ec4\u4ef6\u8c03\u7528\u4e86\u7236\u7ec4\u4ef6\u7684\u65b9\u6cd5\u3002\u7136\u540e\u4f7f\u7528\u4e86\u7b2c\u4e09\u4e2a\u7ec4\u4ef6editableField\uff0c\u8be5\u7ec4\u4ef6\u540c\u6837\u6709\u4e00\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u548cpersonDetail\u7ec4\u4ef6\u4e2d\u5bf9\u5e94\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u8fdb\u884c\u4e86\u7ed1\u5b9a\u3002\u6700\u540e\u589e\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae\uff0c\u5e76\u4f7f\u7528ng-click\u6307\u4ee4\u6307\u5b9a\u4e86\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u3002\n<\/p>\n<h2>\n\teditableField\u7ec4\u4ef6<br \/>\n<\/h2>\n<p>\n\t\u8be5\u7ec4\u4ef6\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5c55\u793a\u5e76\u4fee\u6539person\u5bf9\u8c61\u4e2d\u7684job\u5c5e\u6027\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nmainModule.component(&quot;editableField&quot;, {\r\n  templateUrl: &quot;editableField.html&quot;,\r\n  bindings: {\r\n    fieldValue: &quot;&lt;&quot;,\r\n    onUpdate: &quot;&amp;&quot;\r\n  },\r\n  controller: function() {\r\n    this.$onInit = function() {\r\n      this.editMode = false;\r\n      this.fieldValueCopy = this.fieldValue;\r\n    };\r\n    this.handModelChange = function() {\r\n      if(this.editMode) {\r\n        this.onUpdate({job: &quot;job&quot;, value: this.fieldValue});\r\n        this.fieldValueCopy = this.fieldValue;\r\n      }\r\n      this.editMode = !this.editMode;\r\n    };\r\n    this.reset = function() {\r\n      this.fieldValue = this.fieldValueCopy;\r\n    };\r\n  }\r\n});<\/pre>\n<p>\n\t\u4ece\u6700\u5f00\u59cb\u7684\u8fd0\u884c\u6548\u679c\u4e2d\u53ef\u4ee5\u770b\u5230editableField\u662f\u6709\u5f62\u6001\u53d8\u5316\u7684\uff0c\u6240\u4ee5\u5728$onInit\u51fd\u6570\u4e2d\u5b9a\u4e49\u4e86\u662f\u5426\u4e3a\u7f16\u8f91\u6a21\u5f0f\u7684\u6807\u8bc6\u7b26editMode\u4ee5\u53ca\u4ee3\u8868\u8f93\u5165\u6846\u5185\u5bb9\u7684fieldValue\u53d8\u91cf\uff0c\u56e0\u4e3a\u6709reset\u529f\u80fd\uff0c\u6240\u4ee5\u8fd8\u5b9a\u4e49\u5b58\u50a8\u4fee\u6539\u4e4b\u524d\u503c\u7684\u53d8\u91cffieldValueCopy\u3002\u7136\u540e\u5b9a\u4e49\u4e86\u70b9\u51fbEdit\u6216Save\u6309\u94ae\u89e6\u53d1\u7684\u51fd\u6570handModelChange\uff0c\u5e76\u5728\u8be5\u51fd\u6570\u4e2d\u8c03\u7528\u4e86\u548cpersonDetail\u7ec4\u4ef6\u7684update\u51fd\u6570\u7ed1\u5b9a\u7684onUpdate\u51fd\u6570\uff0c\u540c\u6837\u7531\u5b50\u7ec4\u4ef6\u8c03\u7528\u4e86\u7236\u7ec4\u4ef6\u7684\u65b9\u6cd5\u3002\u8fd8\u5b9a\u4e49\u4e86\u70b9\u51fbReset\u6309\u94ae\u89e6\u53d1\u7684\u51fd\u6570reset\u3002\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;span ng-switch=&quot;$ctrl.editMode&quot;&gt;\r\n  &lt;input ng-switch-when=&quot;true&quot; type=&quot;text&quot; ng-model=&quot;$ctrl.fieldValue&quot;&gt;\r\n  &lt;span ng-switch-default&gt;{{$ctrl.fieldValue}}&lt;\/span&gt;\r\n&lt;\/span&gt;\r\n&lt;button ng-click=&quot;$ctrl.handModelChange()&quot;&gt;{{$ctrl.editMode ? &quot;Save&quot; : &quot;Edit&quot;}}&lt;\/button&gt;\r\n&lt;button ng-if=&quot;$ctrl.editMode&quot; ng-click=&quot;$ctrl.reset()&quot;&gt;Reset&lt;\/button&gt;<\/pre>\n<p>\n\t\u5728editableField.html\u6587\u4ef6\u4e2d\u5c55\u793a\u4e86person\u5bf9\u8c61\u7684job\u5c5e\u6027\uff0c\u5b9a\u4e49\u4e86\u4fee\u6539job\u5c5e\u6027\u7684\u8f93\u5165\u6846\u4ee5\u53ca\u4e24\u4e2a\u6309\u94ae\u3002\u8fd9\u91cc\u51fa\u73b0\u4e86\u4e00\u7ec4\u4e4b\u524d\u6ca1\u89c1\u8fc7\u7684AngularJS\u5185\u7f6e\u6307\u4ee4\uff0cng-switch\u3001ng-switch-when\u3001ng-switch-default\uff0c\u8fd9\u4e09\u4e2a\u6307\u4ee4\u4e00\u822c\u7ec4\u5408\u4f7f\u7528\uff0c\u4f5c\u7528\u7c7b\u4f3cif else\u8bed\u53e5\uff0c\u901a\u8fc7\u8fd9\u7ec4\u6307\u4ee4\u548cdeitMode\u53d8\u91cf\u5c31\u53ef\u4ee5\u8fbe\u5230\u52a8\u6001\u53d8\u6362DOM\u5143\u7d20\u7684\u529f\u80fd\u3002\n<\/p>\n<p>\n\t\u6700\u540e\u6765\u770b\u770b\u7b80\u5355\u7684<code>index.html<\/code>\u6587\u4ef6\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;meta charset=&quot;utf-8&quot;&gt;\r\n    &lt;title&gt;Demo for Directive&lt;\/title&gt;\r\n    &lt;script src=&quot;..\/angular-1.5.8.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;modules.js&quot;&gt;&lt;\/script&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body ng-app=&quot;mainModule&quot;&gt;\r\n    &lt;person-list&gt;&lt;\/person-list&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>\n\t\u4ece\u4e0a\u9762\u7684\u8fd9\u4e2a\u793a\u4f8b\u4e2d\u53ef\u4ee5\u770b\u51fa\u5728editableField\u548cpersonDetail\u7ec4\u4ef6\u4e2d\u90fd\u6ca1\u6709\u771f\u6b63\u610f\u4e49\u4e0a\u53bb\u4fee\u6539\u6570\u636e\u6e90\uff0c\u800c\u662f\u901a\u8fc7\u51fd\u6570\u7ed1\u5b9a\u4e00\u8def\u5c06\u4fee\u6539\u6570\u636e\u6e90\u7684\u884c\u4e3a\u4f20\u9012\u5230\u4e86\u5b9a\u4e49\u6570\u636e\u6e90\u7684\u7ec4\u4ef6personList\u4e2d\uff0c\u7531\u5b83\u6700\u540e\u771f\u6b63\u5b8c\u6210\u5bf9\u6570\u636e\u6e90\u7684\u4fee\u6539\uff0c\u8fd9\u4e5f\u9075\u5faa\u4e86\u7ec4\u4ef6\u4e0d\u5141\u8bb8\u4fee\u6539\u5c5e\u4e8e\u81ea\u5df1\u9694\u79bb\u57df\u4ee5\u5916\u7684\u4efb\u4f55\u6570\u636e\u548cDOM\u5143\u7d20\u7684\u539f\u5219\u3002\n<\/p>\n<p>\n\t<strong>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u672a\u7ecf\u535a\u4e3b\u5141\u8bb8\u4e0d\u5f97\u8f6c\u8f7d\u3002<\/strong><\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=858\">AngularJS\u5165\u95e8\u6559\u7a0b(\u516d)\u7ec4\u4ef6\u5316\u5f00\u53d1<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u63a5\u7740\u4e0a\u4e00\u7ae0AngularJS\u7684\u6307\u4ee4\u5b66\u4e60\uff0c\u672c\u7ae0\u6211\u5c06\u5e26\u9886\u5927\u5bb6\u5b66\u4e60AngularJS\u7684\u7ec4\u4ef6\u3002\u5305\u542b\u53ef\u590d\u7528\u7ec4\u4ef6\u7684\u521b\u5efa\u3001\u7ec4 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[518,529,119,48],"class_list":["post-858","post","type-post","status-publish","format-standard","hentry","category-html5","tag-angularjs","tag-html5","tag-web"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=858"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/858\/revisions"}],"predecessor-version":[{"id":860,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/858\/revisions\/860"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}