{"id":856,"date":"2016-09-21T21:49:11","date_gmt":"2016-09-21T13:49:11","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=856"},"modified":"2016-09-22T21:16:59","modified_gmt":"2016-09-22T13:16:59","slug":"angularjs%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%e4%ba%94","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=856","title":{"rendered":"AngularJS\u5165\u95e8\u6559\u7a0b(\u4e94)\u6307\u4ee4\u7684\u4f5c\u7528\u57df\u548c\u5185\u5d4c\u673a\u5236"},"content":{"rendered":"<p>\n\t\u4e0a\u4e00\u7bc7\u6211\u4eec\u5b66\u4e60\u4e86\u6307\u4ee4\u7684\u7b80\u4ecb\uff0c\u6307\u4ee4\u7684\u5c01\u88c5\uff0c\u6307\u4ee4\u7684\u7c7b\u578b\uff0c\u6307\u4ee4\u5c01\u88c5UI\u7b49\u3002\u4ecb\u4e8e\u6307\u4ee4\u592a\u8fc7\u5f3a\u5927\u4ee5\u53ca\u7bc7\u5e45\u7684\u539f\u56e0\uff0c\u672c\u7ae0\u6211\u5c06\u7ee7\u7eed\u5b66\u4e60\u6307\u4ee4\u7684\u4f5c\u7528\u57df\uff0c\u6307\u4ee4\u7684\u4ea4\u4e92\uff0c\u6307\u4ee4\u64cd\u4f5cDOM\u7b49\u77e5\u8bc6\u3002\n<\/p>\n<h2>\n\t\u6307\u4ee4\u7684\u4f5c\u7528\u57df<br \/>\n<\/h2>\n<p>\n\t\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u914d\u7f6e\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u52a0\u8f7dUI\u6a21\u677f\u6587\u4ef6\uff0c\u4f46\u662f\u6211\u4eec\u65e0\u6cd5\u52a8\u6001\u6307\u5b9aUI\u6a21\u677f\u6587\u4ef6\u4e2d\u663e\u793a\u7684\u5185\u5bb9\u3002\u8fd9\u4e00\u8282\u6211\u4eec\u6765\u4e86\u89e3\u4e00\u4e0b\u5982\u4f55\u901a\u8fc7\u6307\u4ee4\u7684\u9694\u79bb\u57df\u8fbe\u5230\u5728\u540c\u4e00\u4e2a\u6307\u4ee4\u4e2d\u52a8\u6001\u6307\u5b9aUI\u6a21\u677f\u6587\u4ef6\u4e2d\u8981\u663e\u793a\u7684\u5185\u5bb9\uff0c\u5148\u770b\u770b\u4ee3\u7801\u793a\u4f8b\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar mainModule = angular.module(&quot;mainModule&quot;, []);\r\nmainModule.controller(&quot;MyController&quot;, function() {\r\n  this.jason = { name: &quot;Jason&quot;, job: &quot;Developer&quot; };\r\n  this.green = { name: &quot;Green&quot;, job: &quot;Doctor&quot; };\r\n});\r\nmainModule.directive(&quot;myDirective&quot;, function() {\r\n  return {\r\n    restrict: &quot;E&quot;,\r\n    scope: {\r\n      personInfo: &quot;=person&quot;\r\n    },\r\n    templateUrl: &quot;myTemplate.html&quot;\r\n  };\r\n});<\/pre>\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;div ng-controller=&quot;MyController as mc&quot;&gt;\r\n      &lt;my-directive person=&quot;mc.jason&quot;&gt;&lt;\/my-directive&gt;\r\n      &lt;hr&gt;\r\n      &lt;my-directive person=&quot;mc.green&quot;&gt;&lt;\/my-directive&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\nName: {{personInfo.name}}, Job: {{personInfo.job}}<\/pre>\n<p>\n\t\u4ece\u793a\u4f8b\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u7ed9\u6307\u4ee4\u7684\u8fd4\u56de\u5bf9\u8c61\u53c8\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5c5e\u6027scope\uff0c\u8fd9\u5c31\u662f\u6307\u4ee4\u7684\u4f5c\u7528\u57df\u5c5e\u6027\uff0cscope\u5c5e\u6027\u6709\u4e09\u79cd\u53ef\u8bbe\u7f6e\u7684\u503c\uff1a&nbsp;\n<\/p>\n<ul>\n<li>\n\t\tfalse\uff1a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u8868\u793a\u6307\u4ee4\u5171\u4eab\u5b83\u7236\u8282\u70b9\u7684Controller\u7684\u4f5c\u7528\u57df\uff0c\u4e5f\u5c31\u662f\u53ef\u4ee5\u4f7f\u7528{{}}\u8bed\u6cd5\u76f4\u63a5\u8bbf\u95ee\u7236\u8282\u70b9Controller\u4f5c\u7528\u57df\u4e2d\u7684\u5c5e\u6027\u3002&nbsp;\n\t<\/li>\n<li>\n\t\ttrue\uff1a\u521b\u5efa\u6307\u4ee4\u81ea\u5df1\u7684\u4f5c\u7528\u57df\uff0c\u4f46\u662f\u8be5\u4f5c\u7528\u57df\u7ee7\u627f\u7236\u8282\u70b9Controller\u7684\u4f5c\u7528\u57df\u3002&nbsp;\n\t<\/li>\n<li>\n\t\t{}\uff1a\u7b2c\u4e09\u79cd\u662f\u8bbe\u7f6e\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8868\u793a\u521b\u5efa\u4e86\u6307\u4ee4\u81ea\u5df1\u72ec\u7acb\u7684\u4f5c\u7528\u57df\uff0c\u4e0e\u7236\u8282\u70b9Controller\u7684\u4f5c\u7528\u662f\u5b8c\u5168\u9694\u79bb\u7684\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6307\u4ee4\u7684\u9694\u79bb\u4f5c\u7528\u57df\u548c\u7236\u8282\u70b9Controller\u7684\u4f5c\u7528\u57df\u4e4b\u95f4\u8fdb\u884c\u4ea4\u4e92\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u5c06\u4e24\u8005\u8fdb\u884c\u7ed1\u5b9a\uff0c\u8fd9\u91cc\u6709\u4e09\u79cd\u7ed1\u5b9a\u65b9\u5f0f\uff1a&nbsp;\n<\/p>\n<ul>\n<li>\n\t\t\u4f7f\u7528@\u5b9e\u73b0\u5355\u5411\u6570\u636e\u7ed1\u5b9a\uff0c\u4f46\u662f\u53ea\u9650\u4e8e\u7ed1\u5b9aController\u4f5c\u7528\u57df\u4e2d\u503c\u4e3a\u5b57\u7b26\u4e32\u7684\u5c5e\u6027\uff0c\u56e0\u4e3a\u662f\u5355\u5411\u7ed1\u5b9a\uff0c\u6240\u4ee5\u7236\u8282\u70b9Controller\u4fee\u6539\u7ed1\u5b9a\u7684\u5c5e\u6027\u53ef\u5f71\u54cd\u5230\u6307\u4ee4\u4f5c\u7528\u57df\u4e2d\u5bf9\u5e94\u7684\u5c5e\u6027\uff0c\u53cd\u4e4b\u5219\u4e0d\u53ef\u4ee5\u3002\u5728HTML\u4e2d\u4f7f\u7528{{}}\u8bed\u6cd5\u53d6\u503c\uff0c\u6bd4\u5982person=&quot;{{nameStr}}&quot;\u3002&nbsp;\n\t<\/li>\n<li>\n\t\t\u4f7f\u7528=\u5b9e\u73b0\u53cc\u5411\u6570\u636e\u7ed1\u5b9a\uff0c\u5728\u7236\u8282\u70b9Controller\u4e2d\u4fee\u6539\u5c5e\u6027\u548c\u5728\u6307\u4ee4\u4e2d\u4fee\u6539\u5c5e\u6027\u53ef\u76f8\u4e92\u5f71\u54cd\u3002\u5728HTML\u4e2d\u76f4\u63a5\u4f7f\u7528\u5c5e\u6027\u540d\u79f0\uff0c\u6bd4\u5982person=&quot;jasonObj&quot;\u3002&nbsp;\n\t<\/li>\n<li>\n\t\t\u4f7f\u7528&amp;\u5b9e\u73b0\u51fd\u6570\u7ed1\u5b9a\uff0c\u7528\u4e8e\u7ed1\u5b9aController\u4e2d\u503c\u4e3a\u51fd\u6570\u7684\u5c5e\u6027\uff0c\u5728HTML\u4e2d\u76f4\u63a5\u8c03\u7528\u51fd\u6570\uff0c\u6bd4\u5982action=&quot;click()&quot;\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\u6211\u4eec\u7ed9myDirective\u6307\u4ee4\u8bbe\u7f6e\u4e86\u9694\u79bb\u57df\u5e76\u6dfb\u52a0\u4e86\u540d\u4e3apersonInfo\u7684\u5c5e\u6027\uff0c\u5e76\u4e0e\u7236\u8282\u70b9\u7684MyController\u8fdb\u884c\u6570\u636e\u53cc\u5411\u7ed1\u5b9a\uff0c\u5728HTML\u4ee3\u7801\u4e2d\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7&lt;my-directive&gt;\u6307\u4ee4\u6807\u7b7e\u7684person\u5c5e\u6027\u4e0eMyController\u7684\u6570\u636e\u7ed1\u5b9a\u4e86\u3002\u53e6\u5916\uff0c\u5728\u8fdb\u884c\u7ed1\u5b9a\u65f6\u8fd8\u6709\u4e00\u79cd\u7b80\u5199\u7684\u65b9\u5f0f\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\n...\r\nscope: {\r\n  personInfo: &quot;=&quot;\r\n  \/\/ personInfo: &quot;@&quot;\r\n  \/\/ personInfo: &quot;&amp;&quot;\r\n},\r\n...<\/pre>\n<p>\n\t\u7b49\u540c\u4e8e\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\n...\r\nscope: {\r\n  personInfo: &quot;=personInfo&quot;\r\n  \/\/ personInfo: &quot;@personInfo&quot;\r\n  \/\/ personInfo: &quot;&amp;personInfo&quot;\r\n},\r\n...<\/pre>\n<h2>\n\t\u6307\u4ee4\u7684Controller<br \/>\n<\/h2>\n<p>\n\t\u5728\u6307\u4ee4\u4e2d\u4e5f\u53ef\u4ee5\u521b\u5efaController\uff0c\u548c\u5728Module\u4e2d\u521b\u5efaController\u5f88\u7c7b\u4f3c\uff0c\u65e2\u5b9a\u4e49\u51fd\u6570\uff0c\u5728\u53c2\u6570\u4e2d\u6ce8\u5165\u9700\u8981\u7684AngularJS\u670d\u52a1\u65e2\u53ef\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar mainModule = angular.module(&quot;mainModule&quot;, []);\r\nmainModule.controller(&quot;MyController&quot;, function($scope) {\r\n  $scope.green = { name: &quot;Green&quot;, job: &quot;Doctor&quot; };\r\n});\r\nmainModule.directive(&quot;myDirective&quot;, function() {\r\n  return {\r\n    restrict: &quot;E&quot;,\r\n    scope: {\r\n      person: &quot;=&quot;\r\n    },\r\n    controller: function($scope) {\r\n      $scope.jason = { name: &quot;Jason&quot;, job: &quot;Developer&quot; };\r\n    },\r\n    templateUrl: &quot;myTemplate.html&quot;\r\n  };\r\n});<\/pre>\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;div ng-controller=&quot;MyController&quot;&gt;\r\n      &lt;my-directive person=&quot;green&quot;&gt;&lt;\/my-directive&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\nName: {{jason.name}}, Job: {{jason.job}}\r\nName: {{person.name}}, Job: {{person.job}}<\/pre>\n<p>\n\t\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7ed9myDirective\u6307\u4ee4\u6dfb\u52a0\u4e86Controller\uff0c\u6709\u4e00\u70b9\u4e0d\u540c\u7684\u662f\u5728\u6dfb\u52a0Controller\u65f6\u4e0d\u80fd\u8bbe\u7f6e\u540d\u79f0\uff0c\u6307\u4ee4\u7684Controller\u540d\u79f0\u9ed8\u8ba4\u4e0e\u6307\u4ee4\u540d\u79f0\u4e00\u6837\uff0c\u5982\u679c\u9700\u8981\u53e6\u5916\u6307\u5b9a\u540d\u79f0\uff0c\u9700\u8981\u914d\u7f6econtrollerAs\u6307\u5b9aController\u7684\u540d\u79f0\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\n...\r\ncontroller: function($scope) {\r\n  $scope.jason = { name: &quot;Jason&quot;, job: &quot;Developer&quot; };\r\n},\r\ncontrollerAs: &quot;directiveController&quot;,\r\n...<\/pre>\n<p>\n\t\u5728\u4e0a\u9762\u793a\u4f8b\u7684UI\u6a21\u677f\u6587\u4ef6\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u65e2\u53ef\u4ee5\u4f7f\u7528\u6307\u4ee4\u9694\u79bb\u57df\u4e2d\u4e0e\u7236\u8282\u70b9Controller\u7ed1\u5b9a\u7684\u5c5e\u6027\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5728\u6307\u4ee4\u81ea\u5df1\u7684Controller\u4e2d\u5b9a\u4e49\u5728\u9694\u79bb\u57df\u7684\u5c5e\u6027\u3002\n<\/p>\n<p>\n\t\u6307\u4ee4\u4e4b\u95f4\u7684\u4ea4\u4e92\n<\/p>\n<ul>\n<li>\n\t\trequire: &quot;controllerName&quot;\uff1a\u53ea\u67e5\u627e\u6307\u4ee4\u81ea\u5df1\u7684Controller\u3002&nbsp;\n\t<\/li>\n<li>\n\t\trequire: &quot;^controllerName&quot;\uff1a\u67e5\u627e\u6307\u4ee4\u81ea\u5df1\u7684Controller\u4ee5\u53ca\u7236\u6307\u4ee4\u7684Controller\u3002&nbsp;\n\t<\/li>\n<li>\n\t\trequire: &quot;^^controllerName&quot;\uff1a\u53ea\u67e5\u627e\u7236\u6307\u4ee4\u7684Controller\u3002&nbsp;\n\t<\/li>\n<li>\n\t\trequire: [&#8220;^controllerName1&#8221;, &#8220;^controllerName2&#8221;]\uff1a\u5f15\u7528\u591a\u4e2aController\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u5982\u679c\u6307\u4ee4\u67e5\u627e\u5230\u5f15\u7528\u7684Controller\u540e\u8be5\u5982\u4f55\u4f7f\u7528\u5462\uff0c\u8fd9\u5c31\u8981\u4f7f\u7528\u6307\u4ee4\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u7684\u5c5e\u6027link\u51fd\u6570\u4e86\u3002link\u51fd\u6570\u4e3b\u8981\u7528\u6765\u4e3aDOM\u5143\u7d20\u6dfb\u52a0\u4e8b\u4ef6\u76d1\u542c\u3001\u76d1\u89c6\u6a21\u578b\u5c5e\u6027\u53d8\u5316\u3001\u4ee5\u53ca\u66f4\u65b0DOM\uff0c\u8be5\u51fd\u6570\u5171\u6709\u4e94\u4e2a\u53c2\u6570\uff1a&nbsp;\n<\/p>\n<ul>\n<li>\n\t\tscope\uff1a\u6307\u4ee4\u7684\u4f5c\u7528\u57df\uff0c\u9ed8\u8ba4\u662f\u7236\u8282\u70b9Controller\u7684\u4f5c\u7528\u57df\uff0c\u5982\u679c\u6307\u4ee4\u6709\u521b\u5efa\u81ea\u5df1\u7684\u4f5c\u7528\u57df\uff0c\u90a3\u4e48\u5219\u6307\u6307\u4ee4\u81ea\u5df1\u7684\u4f5c\u7528\u57df\u3002&nbsp;\n\t<\/li>\n<li>\n\t\telement\uff1a\u6307\u4ee4\u7684jQLite(jQuery\u7684\u5b50\u96c6)\u5305\u88c5\u7684DOM\u5143\u7d20\uff0c\u53ef\u4ee5\u901a\u8fc7\u8be5\u53c2\u6570\u64cd\u4f5c\u6307\u4ee4\u6240\u5728\u7684DOM\u5143\u7d20\u3002&nbsp;\n\t<\/li>\n<li>\n\t\tattrs\uff1a\u6307\u4ee4\u6240\u5728DOM\u5143\u7d20\u7684\u5c5e\u6027\u5bf9\u8c61\uff0c\u901a\u8fc7.\u8bed\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5230\u7ed9DOM\u5143\u7d20\u6dfb\u52a0\u7684\u5c5e\u6027\u3002&nbsp;\n\t<\/li>\n<li>\n\t\tcontroller\uff1a\u6307\u4ee4\u901a\u8fc7require\u5c5e\u6027\u5f15\u7528\u7684Controller\u5b9e\u4f8b\u3002&nbsp;\n\t<\/li>\n<li>\n\t\ttranscludeFn\uff1a\u5d4c\u5165\u51fd\u6570\n\t<\/li>\n<\/ul>\n<p>\n\tlink\u51fd\u6570\u7684\u5176\u4ed6\u51e0\u4e2a\u53c2\u6570\u540e\u9762\u6587\u7ae0\u4e2d\u90fd\u4f1a\u8bb2\u5230\uff0c\u5f53\u6307\u4ee4\u627e\u5230\u901a\u8fc7require\u5c5e\u6027\u5f15\u7528\u7684Controller\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7link\u51fd\u6570\u7684\u7b2c\u56db\u4e2a\u53c2\u6570\u8bbf\u95ee\u5f15\u7528\u7684Controller\u4e86\u3002\n<\/p>\n<p>\n\t\u901a\u8fc7\u6307\u4ee4\u64cd\u4f5cDOM\u5143\u7d20\n<\/p>\n<p>\n\t\u6211\u4eec\u4e86\u89e3\u4e86link\u51fd\u6570\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u8be5\u51fd\u6570\u5b9e\u73b0\u5404\u79cd\u6709\u7528\u7684\u6307\u4ee4\u4e86\uff0c\u6bd4\u5982\u901a\u8fc7\u6307\u4ee4\u64cd\u4f5cDOM\u5143\u7d20\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar mainModule = angular.module(&quot;mainModule&quot;, []);\r\nmainModule.directive(&quot;myDirective&quot;, function($interval) {\r\n  return {\r\n    restrict: &quot;A&quot;,\r\n    link: function(scope, element, attrs) {\r\n      $interval(function() {\r\n        element.text(new Date());\r\n      }, 1000);\r\n    }\r\n  };\r\n});<\/pre>\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;div&gt;\r\n      Current Date is: &lt;span my-directive&gt;&lt;\/span&gt;\r\n    &lt;\/div&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>\n\t\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u6211\u4eec\u9650\u5b9a\u4e86myDirective\u6307\u4ee4\u53ea\u80fd\u4ee5\u6807\u7b7e\u5c5e\u6027\u7684\u5f62\u5f0f\u4f7f\u7528\uff0c\u7136\u540e\u6ce8\u5165\u4e86AngularJS\u7684\u5185\u7f6e\u670d\u52a1$interval\uff0c\u901a\u8fc7link\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u83b7\u53d6\u5230\u6307\u4ee4\u6240\u5728\u7684DOM\u5143\u7d20\uff0c\u7136\u540e\u5468\u671f\u6027\u66f4\u65b0DOM\u5143\u7d20\u663e\u793a\u7684\u5185\u5bb9\u3002<br \/>\n\t<img decoding=\"async\" alt=\"Angular\u6307\u4ee4\u64cd\u4f5cDOM\" src=\"http:\/\/7xpp8a.com1.z0.glb.clouddn.com\/angularjs\/angularjs-guide-4.gif\" \/>\n<\/p>\n<h2>\n\t\u6307\u4ee4\u7684\u5185\u5d4c\u673a\u5236<br \/>\n<\/h2>\n<p>\n\t\u5927\u5bb6\u90fd\u77e5\u9053HTML\u4e2d\u7684DOM\u5143\u7d20\u662f\u5177\u6709\u5c42\u7ea7\u5173\u7cfb\u7684\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u6211\u4eec\u4f7f\u7528\u6307\u4ee4\u5c01\u88c5\u7684UI\u6a21\u677f\u9897\u7c92\u5ea6\u90fd\u4f1a\u6bd4\u8f83\u5c0f\uff0c\u6240\u4ee5\u5c31\u4f1a\u51fa\u73b0\u6307\u4ee4\u5d4c\u5957\u7684\u73b0\u8c61\uff0c\u8fd9\u51e0\u9700\u8981\u7528\u5230\u6307\u4ee4\u7684\u5185\u5d4c\u673a\u5236\u4e86\uff0c\u6307\u4ee4\u7684transclude\u5c5e\u6027\u9ed8\u8ba4\u4e3afalse\uff0c\u5982\u679c\u5c06\u5176\u8bbe\u7f6e\u4e3atrue\uff0c\u90a3\u4e48\u8be5\u6307\u4ee4\u5c31\u5f00\u542f\u4e86\u5185\u5d4c\u673a\u5236\uff0c\u4e5f\u5c31\u662f\u8bf4\u6307\u4ee4\u6807\u7b7e\u4e4b\u95f4\u7684\u5185\u5bb9\u53ef\u4ee5\u88ab\u6307\u5b9a\u5d4c\u5165UI\u6a21\u677f\u4e2d\u88abng-transclude\u5185\u7f6e\u6307\u4ee4\u6807\u8bb0\u8fc7\u7684DOM\u5143\u7d20\u4e2d\uff0c\u7ed3\u5408\u4e4b\u524d\u8bf4\u8fc7\u7684\u7236\u5b50\u6307\u4ee4\u4ea4\u4e92\u7684\u5185\u5bb9\u6765\u5b9e\u73b0\u4e00\u4e2a\u4f8b\u5b50\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\n\/\/ modules.js\r\nvar mainModule = angular.module(&quot;mainModule&quot;, []);\r\nmainModule.directive(&quot;myTabs&quot;, function() {\r\n  return {\r\n    restrict: &quot;E&quot;,\r\n    transclude: true,\r\n    controller: function($scope) {\r\n      $scope.panes = [];\r\n      var panes = $scope.panes;\r\n      this.addPane = function(pane) {\r\n        if(panes.length == 0) {\r\n          $scope.select(pane);\r\n        };\r\n        panes.push(pane);\r\n      };\r\n      $scope.select = function(pane) {\r\n        angular.forEach(panes, function(pane) {\r\n          pane.selected = false;\r\n        });\r\n        pane.selected = true;\r\n      };\r\n    },\r\n    templateUrl: &quot;myTabs.html&quot;\r\n  };\r\n});\r\nmainModule.directive(&quot;myPane&quot;, function() {\r\n  return {\r\n    restrict: &quot;E&quot;,\r\n    require: &quot;^^myTabs&quot;,\r\n    scope: {\r\n      name: &quot;@&quot;,\r\n      job: &quot;@&quot;\r\n    },\r\n    link: function(scope, element, attrs, controller) {\r\n      controller.addPane(scope);\r\n    },\r\n    templateUrl: &quot;myPane.html&quot;\r\n  };\r\n})<\/pre>\n<p>\n\t\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u6307\u4ee4\uff0cmyTabs\u548cmyPane\uff0c\u5728myTabs\u6307\u4ee4\u4e2d\uff0c\u6211\u4eec\u9650\u5b9a\u5b83\u53ea\u80fd\u4ee5\u6807\u7b7e\u5f62\u5f0f\u4f7f\u7528\uff0c\u5f00\u542f\u4e86\u5185\u5d4c\u673a\u5236\uff0c\u5e76\u5b9a\u4e49\u4e86\u5b83\u81ea\u5df1\u7684Controller\uff0c\u5728Controller\u4e2d\u5b9a\u4e49\u4e86panes\u53d8\u91cf\u548caddPane(pane)\uff0cselect(pane)\u4e24\u4e2a\u65b9\u6cd5\uff0c\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u5185\u5bb9\u8fd9\u91cc\u5c31\u4e0d\u89e3\u91ca\u4e86\uff0c\u90fd\u5f88\u7b80\u5355\uff0c\u6700\u540e\u6307\u5b9a\u4e86UI\u6a21\u677f\u6587\u4ef6myTabs.html\u3002\n<\/p>\n<p>\n\t\u5728myPane\u6307\u4ee4\u4e2d\u540c\u6837\u9650\u5b9a\u53ea\u80fd\u4ee5\u6807\u7b7e\u5f62\u5f0f\u4f7f\u7528\uff0c\u6307\u5b9a\u4e86\u8981\u5f15\u7528\u7684\u7236\u8282\u70b9\u7684Controller\u540d\u79f0\uff0c\u540e\u521b\u5efa\u4e86\u81ea\u5df1\u7684\u9694\u79bb\u57df\uff0c\u5b9a\u4e49\u4e86name\uff0cjob\u4e24\u4e2a\u5c5e\u6027\uff0c\u5e76\u8fdb\u884c\u4e86\u5b57\u7b26\u4e32\u7684\u5355\u5411\u7ed1\u5b9a\uff0c\u7136\u540e\u5b9a\u4e49\u4e86link\u51fd\u6570\uff0c\u901a\u8fc7\u7b2c\u56db\u4e2a\u53c2\u6570\u8bbf\u95ee\u5230\u4e86\u7236\u8282\u70b9\u7684myTabsController\uff0c\u5e76\u8c03\u7528addPane(pane)\u51fd\u6570\uff0c\u5c06\u81ea\u5df1\u7684\u9694\u79bb\u57df\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u6700\u540e\u6307\u5b9a\u4e86UI\u6a21\u677f\u6587\u4ef6myPane.html\u3002\n<\/p>\n<p>\n\t\u518d\u6765\u770b\u770bindex.html\u548cmyTabs.html\uff0cmyPane.html\u8fd9\u4e24\u4e2a\u6a21\u677f\u6587\u4ef6\uff1a\n<\/p>\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 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;my-tabs&gt;\r\n      &lt;my-pane name=&quot;Jason&quot; job=&quot;Developer&quot;&gt;&lt;\/my-pane&gt;\r\n      &lt;my-pane name=&quot;Green&quot; job=&quot;Doctor&quot;&gt;&lt;\/my-pane&gt;\r\n    &lt;\/my-tabs&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\n&lt;!--myTabs.html--&gt;\r\n&lt;div&gt;\r\n  &lt;ul&gt;\r\n    &lt;li ng-repeat=&quot;pane in panes&quot;&gt;\r\n      &lt;a href=&quot;&quot; ng-click=&quot;select(pane)&quot;&gt;{{pane.name}}&lt;\/a&gt;\r\n    &lt;\/li&gt;\r\n  &lt;\/ul&gt;\r\n  &lt;div id=&quot;paneContainer&quot; ng-transclude&gt;&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;!--myPane.html--&gt;\r\n&lt;div ng-show=&quot;selected&quot;&gt;\r\n  I am {{name}}, my job is {{job}}!\r\n&lt;\/div&gt;<\/pre>\n<p>\n\t\u5728index.html\u4e2d\uff0cmyTabs\u6307\u4ee4\u5305\u542b\u4e24\u4e2amyPane\u6307\u4ee4\uff0c\u8fd9\u4e24\u4e2amyPane\u6307\u4ee4\u6240\u663e\u793a\u7684\u5185\u5bb9\u5c06\u5d4c\u5165\u5728myTabs.html\u4e2did\u4e3apaneContainer\u7684DIV\u4e2d\uff0c\u4e5f\u5c31\u662fmyPane.html\u4e2d\u7684\u5185\u5bb9\u4f1a\u88ab\u5d4c\u5165\u5728\u8fd9\u4e2aDIV\u91cc\u3002\n<\/p>\n<p>\n\t\u4e0a\u9762\u8fd9\u4e09\u4e2a\u6587\u4ef6\u4e2d\u6709\u51e0\u4e2a\u70b9\u9700\u8981\u6ce8\u610f\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\u56e0\u4e3a\u5728myPane\u6307\u4ee4\u7684\u9694\u79bb\u57df\u4e2d\u5b9a\u4e49\u4e86name\u548cjob\u5c5e\u6027\uff0c\u5e76\u8fdb\u884c\u4e86\u5b57\u7b26\u4e32\u7ed1\u5b9a\uff0c\u6240\u4ee5\u5728index.html\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u5bf9myPane\u6807\u7b7e\u91cc\u7684name\uff0cjob\u5c5e\u6027\u76f4\u63a5\u8fdb\u884c\u5b57\u7b26\u4e32\u8d4b\u503c\u3002&nbsp;\n\t<\/li>\n<li>\n\t\t\u56e0\u4e3a\u5728myPane\u6307\u4ee4\u4e2d\u5f15\u7528\u4e86myTabs\u6307\u4ee4\u7684Controller\uff0c\u5e76\u5728link\u51fd\u6570\u4e2d\u5c06\u9694\u79bb\u57df\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u4e86myTabs\uff0c\u65e2myTabs\u6307\u4ee4\u7684Controller\u4e2d\u7684select(pane)\u548caddPane(pane)\u51fd\u6570\u7684\u53c2\u6570\u5747\u4e3amyPane\u6307\u4ee4\u7684\u9694\u79bb\u57df\uff0c\u6240\u4ee5\u5728myTabs.html\u6587\u4ef6\u4e2d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528pane\u8bbf\u95eemyPane\u6307\u4ee4\u9694\u79bb\u57df\u4e2d\u5b9a\u4e49\u7684\u5c5e\u6027\uff0c\u6bd4\u5982{{pane.name}}\uff0c\u5e76\u4e14\u4e5f\u53ef\u4ee5\u5728myTabs\u6307\u4ee4\u5728myPane\u7684\u9694\u79bb\u57df\u4e2d\u5b9a\u4e49\u5c5e\u6027\uff0c\u6bd4\u5982pane.selected = true\uff0c\u7ed9\u9694\u79bb\u57df\u5b9a\u4e49\u4e86selected\u7684\u5c5e\u6027\uff0c\u7136\u540e\u53ef\u4ee5\u5728myPane\u6307\u4ee4\u4e2d\u4f7f\u7528\u3002&nbsp;\n\t<\/li>\n<li>\n\t\tng-show\u662fAngularJS\u5185\u7f6e\u7684\u6307\u4ee4\uff0c\u7528\u4e8e\u663e\u793a\u6216\u9690\u85cf\u6307\u5b9a\u7684DOM\u5143\u7d20\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u4e0a\u9762demo\u7684\u8fd0\u884c\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n\t<img decoding=\"async\" alt=\"AngularJS\u5185\u5d4c\u6307\u4ee4\" src=\"http:\/\/7xpp8a.com1.z0.glb.clouddn.com\/angularjs\/angularjs-guide-5.gif\" \/>\n<\/p>\n<p>\n\tlink\u51fd\u6570\u7684\u7b2c\u4e94\u4e2a\u53c2\u6570transcludeFn\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5e38\u7528\u7684\u6709\u4e24\u4e2a\u53c2\u6570scope\u548cfunction(clone){}\uff0c\u65e2transcludeFn(scope, function(clone){})\u3002\u524d\u8005\u662f\u5d4c\u5165\u5185\u5bb9\u7684\u4f5c\u7528\u57df\uff0c\u4e0e\u6307\u4ee4\u7684\u9694\u79bb\u4f5c\u7528\u57df\u662f\u5e73\u884c\u7684\uff0c\u540e\u8005\u51fd\u6570\u7684\u53c2\u6570clone\u662f\u5d4c\u5165\u7684\u5185\u5bb9\u7684jquery\u5c01\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u5bf9\u5d4c\u5165\u7684\u5185\u5bb9\u8fdb\u884cDOM\u64cd\u4f5c\u3002\n<\/p>\n<h2>\n\t\u6307\u4ee4\u7684\u5176\u4ed6\u5c5e\u6027<br \/>\n<\/h2>\n<p>\n\t<strong>priority<\/strong>\u7528\u4e8e\u6307\u5b9a\u6307\u4ee4\u7684\u4f18\u5148\u7ea7\uff0c\u8be5\u5c5e\u6027\u7684\u503c\u4ece1\u5f00\u59cb\u3002\u5f53\u6709\u591a\u4e2a\u6307\u4ee4\u5b9a\u4e49\u5728\u540c\u4e00\u4e2aDOM\u5143\u7d20\u4e2d\u65f6\u5c31\u9700\u8981\u901a\u8fc7\u8be5\u5c5e\u6027\u660e\u786e\u5b83\u4eec\u7684\u6267\u884c\u987a\u5e8f\u3002<br \/>\n\t<strong>replace<\/strong>\u7528\u4e8e\u5224\u5b9a\u662f\u5426\u5c06UI\u6a21\u677f\u7684\u5185\u5bb9\u66ff\u6362\u6389\u6307\u4ee4\u6807\u7b7e\u672c\u8eab\uff0c\u8be5\u5c5e\u6027\u9ed8\u8ba4\u503c\u4e3afalse\uff0c\u65e2\u4fdd\u7559\u6307\u4ee4\u6807\u7b7e\u672c\u8eab\uff0c\u82e5\u8bbe\u7f6e\u4e3atrue\u5219\u66ff\u6362\u6307\u4ee4\u6807\u7b7e\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=856\">AngularJS\u5165\u95e8\u6559\u7a0b(\u4e94)\u6307\u4ee4\u7684\u4f5c\u7528\u57df\u548c\u5185\u5d4c\u673a\u5236<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u6211\u4eec\u5b66\u4e60\u4e86\u6307\u4ee4\u7684\u7b80\u4ecb\uff0c\u6307\u4ee4\u7684\u5c01\u88c5\uff0c\u6307\u4ee4\u7684\u7c7b\u578b\uff0c\u6307\u4ee4\u5c01\u88c5UI\u7b49\u3002\u4ecb\u4e8e\u6307\u4ee4\u592a\u8fc7\u5f3a\u5927\u4ee5\u53ca\u7bc7\u5e45\u7684\u539f\u56e0\uff0c\u672c\u7ae0\u6211\u5c06\u7ee7 [&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,528,119,48],"class_list":["post-856","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\/856","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=856"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/856\/revisions"}],"predecessor-version":[{"id":861,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/856\/revisions\/861"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}