>  기사  >  웹 프론트엔드  >  AngularJS에서 사용자 정의 명령 및 명령 구성 항목을 구현하는 방법에 대한 자세한 설명

AngularJS에서 사용자 정의 명령 및 명령 구성 항목을 구현하는 방법에 대한 자세한 설명

小云云
小云云원래의
2018-01-16 17:26:521243검색

이 글은 주로 AngularJS에서 사용자 정의 명령을 구현하는 방법과 명령 구성 항목을 소개합니다. AngularJS 사용자 정의 명령과 명령 구성 항목의 구현 기술을 예제 형식으로 간략하게 요약하고 분석합니다. 그것은 모두에게 도움이 될 수 있습니다.

AngularJS 사용자 정의 지시어는 두 가지 방법으로 작성할 수 있습니다:


//第一种
angular.module('MyApp',[])
.directive('zl1',zl1)
.controller('con1',['$scope',func1]);
function zl1(){
  var directive={
    restrict:'AEC',
   template:'this is the it-first directive',
  };
  return directive;
};
function func1($scope){
  $scope.name="alice";
}
//第二种
angular.module('myApp',[]).directive('zl1',[ function(){
 return {
  restrict:'AE',
  template:'thirective',
  link:function($scope,elm,attr,controller){
   console.log("这是link");
  },
  controller:function($scope,$element,$attrs){
   console.log("这是con");
  }
 };
}]).controller('Con1',['$scope',function($scope){
 $scope.name="aliceqqq";
}]);

지시어 구성 항목

angular.module('myApp', []).directive('first', [ function(){
  return {
    // scope: false, // 默认值,共享父级作用域
    // controller: function($scope, $element, $attrs, $transclude) {},
    restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
    template: 'first name:{{name}}',
  };
}]).directive('second', [ function(){
  return {
    scope: true, // 继承父级作用域并创建指令自己的作用域
    // controller: function($scope, $element, $attrs, $transclude) {},
    restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
    //当修改这里的name时,second会在自己的作用域中新建一个name变量,与父级作用域中的
    // name相对独立,所以再修改父级中的name对second中的name就不会有影响了
    template: 'second name:{{name}}',
  };
}]).directive('third', [ function(){
  return {
    scope: {}, // 创建指令自己的独立作用域,与父级毫无关系
    // controller: function($scope, $element, $attrs, $transclude) {},
    restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
    template: 'third name:{{name}}',
  };
}])
.controller('DirectiveController', ['$scope', function($scope){
  $scope.name="mike";
}]);

관련 권장 사항:

vue-cli 사용자 정의 지시어 지시어가 추가되었습니다. 검증에 대한 자세한 설명 슬라이더

Vue의 사용자 정의 지시어를 사용하여 드롭다운 메뉴를 완성하는 방법

Angularjs의 사용자 정의 지시어에 대한 자세한 소개 Directive

위 내용은 AngularJS에서 사용자 정의 명령 및 명령 구성 항목을 구현하는 방법에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.