码迷,mamicode.com
首页 > 其他好文 > 详细

angular 自定义指令

时间:2015-09-22 10:08:48      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

Template-expanding directive:

<div ng-controller="Controller">
  <div my-customer></div>
</div>

angular.module(‘docsSimpleDirective‘, [])
.controller(‘Controller‘, [‘$scope‘, function($scope) {
  $scope.customer = {
    name: ‘Naomi‘,
    address: ‘1600 Amphitheatre‘
  };
}])
.directive(‘myCustomer‘, function() {
  return {
    template: ‘Name: {{customer.name}} Address: {{customer.address}}‘
  };
});

结果:Name: Naomi Address: 1600 Amphitheatre
<div ng-controller="Controller">
  <div my-customer></div>
</div>

angular.module(‘docsTemplateUrlDirective‘, [])
.controller(‘Controller‘, [‘$scope‘, function($scope) {
  $scope.customer = {
    name: ‘Naomi‘,
    address: ‘1600 Amphitheatre‘
  };
}])
.directive(‘myCustomer‘, function() {
  return {
    templateUrl: ‘my-customer.html‘
  };
});

my-customer.html:

Name: {{customer.name}} Address: {{customer.address}}

结果:Name: Naomi Address: 1600 Amphitheatre

  

<div ng-controller="Controller">
  <div my-customer type="name"></div>
  <div my-customer type="address"></div>
</div>

angular.module(‘docsTemplateUrlDirective‘, [])
.controller(‘Controller‘, [‘$scope‘, function($scope) {
  $scope.customer = {
    name: ‘Naomi‘,
    address: ‘1600 Amphitheatre‘
  };
}])
.directive(‘myCustomer‘, function() {
  return {
    templateUrl: function(elem, attr){
      return ‘customer-‘+attr.type+‘.html‘;
    }
  };
});

customer-name.html:
Name: {{customer.name}}

customer-address.html:
Address: {{customer.address}}

结果:Name: Naomi
Address: 1600 Amphitheatre

  

The restrict option is typically set to:

‘A‘ - only matches attribute name
‘E‘ - only matches element name
‘C‘ - only matches class name
These restrictions can all be combined as needed:

‘AEC‘ - matches either attribute or element or class name

<div ng-controller="Controller">
  <my-customer></my-customer>
</div>

angular.module(‘docsRestrictDirective‘, [])
.controller(‘Controller‘, [‘$scope‘, function($scope) {
  $scope.customer = {
    name: ‘Naomi‘,
    address: ‘1600 Amphitheatre‘
  };
}])
.directive(‘myCustomer‘, function() {
  return {
    restrict: ‘E‘,
    templateUrl: ‘my-customer.html‘
  };
});

my-customer.html:
Name: {{customer.name}} Address: {{customer.address}}

结果:Name: Naomi Address: 1600 Amphitheatre

  

Isolating the Scope of a Directive:

<div ng-controller="NaomiController">
  <my-customer></my-customer>
</div>
<hr>
<div ng-controller="IgorController">
  <my-customer></my-customer>
</div>

angular.module(docsScopeProblemExample, [])
.controller(NaomiController, [$scope, function($scope) {
  $scope.customer = {
    name: Naomi,
    address: 1600 Amphitheatre
  };
}])
.controller(IgorController, [$scope, function($scope) {
  $scope.customer = {
    name: Igor,
    address: 123 Somewhere
  };
}])
.directive(myCustomer, function() {
  return {
    restrict: E,
    templateUrl: my-customer.html
  };
});

my-customer.html:
Name: {{customer.name}} Address: {{customer.address}}
结果:
Name: Naomi Address: 1600 Amphitheatre



Name: Igor Address: 123 Somewhere

 

angular 自定义指令

标签:

原文地址:http://www.cnblogs.com/xiaotaiyang/p/4828022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!