how to write a directive in angularjs

前端 未结 1 664
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 22:41

i like to make a custom component using directive. i checked lot of tutorials and its get confusing me can anyone explain how a directive works. the component i am planing t

1条回答
  •  隐瞒了意图╮
    2020-12-28 23:19

    Here's your directive, with some inline comments:

    angular.module( 'directives', [] ).directive( 'shoutList', function () {
      return {
        restrict: 'E', // allow as an element; the default is only an attribute
        scope: {       // create an isolate scope
          shouts: '='  // map the var in the shouts attribute to this scope
        },
        templateUrl: 'templates/shoutList.html', // load the template file
        controller: function ( $scope ) {
          // we declare a your function for use in the view
          $scope.deleteShout = function ( idx, id ) {
            // do whatever
          };
        }
      };
    });
    

    And the template file:

    {{shout.message}}

    And now you can use it in your code, like so:

    Controller:

    .controller( 'MainCtrl', function ( $scope ) {
      $scope.myShouts = // ...
    });
    

    View:

    
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题