Angularjs 如何制作自定义指令?

Angularjs 如何制作自定义指令?,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我是angularjs的新手,现在正在构建我的第一个angular应用程序。我想使用自定义指令在表格中显示我现在显示的数据。 谁能告诉我怎么做? 我只想有一个自定义指令,所有数据都应该使用该指令显示。 自定义指令是否应放在单独的文件中? 请指导我怎么做 这是我的控制器: 'use strict'; app.controller('myAppCtrl', function ($scope, $http) { $scope.names = [] $http.get('https://www

我是angularjs的新手,现在正在构建我的第一个angular应用程序。我想使用自定义指令在表格中显示我现在显示的数据。 谁能告诉我怎么做? 我只想有一个自定义指令,所有数据都应该使用该指令显示。 自定义指令是否应放在单独的文件中? 请指导我怎么做

这是我的控制器:

'use strict';

 app.controller('myAppCtrl', function ($scope, $http) {
 $scope.names = []

 $http.get('https://www.reddit.com/r/worldnews/new.json')
     .success(function (response) {
     $scope.names = response.data.children;
 })
});

你应该浏览一些在线资源来学习角度指令

面向初学者的简单指导方法 //控制器

 app.controller('MainCtrl', function($scope) {
 $scope.name = 'World';
 });
//指示

app.directive('simpleDemo',function(){
var newtemplate = function(){
 var template = '<i class="glyphicon glyphicon-remove"><i>';
 return template;
 }
return {
restrict: 'E',
template: newtemplate
}
})
//html

<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<button><simple-demo></simple-demo></button>
它可能会帮助你

    <html>
    <head>
       <title>Angular JS Custom Directives</title>
    </head>

       <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
       <script>
          var mainApp = angular.module("mainApp", []);

          mainApp.directive('student', function() {
             var directive = {};
             directive.restrict = 'E';
             directive.template = "Student: <b>{{student.name}}</b> , Roll No: <b>{{student.rollno}}</b>";

             directive.scope = {
                student : "=name"
             }

             directive.compile = function(element, attributes) {
                element.css("border", "1px solid #cccccc");

                var linkFunction = function($scope, element, attributes) {
                   element.html("Student: <b>"+$scope.student.name +"</b> , Roll No: <b>"+$scope.student.rollno+"</b><br/>");
                   element.css("background-color", "#ff00ff");
                }

                return linkFunction;
             }

             return directive;
          });

          mainApp.controller('StudentController', function($scope) {
                $scope.Mahesh = {};
                $scope.Mahesh.name = "Mahesh Parashar";
                $scope.Mahesh.rollno  = 1;

                $scope.Piyush = {};
                $scope.Piyush.name = "Piyush Parashar";
                $scope.Piyush.rollno  = 2;
          });

       </script>
<body>
       <h2>AngularJS Sample Application</h2>
       <div ng-app="mainApp" ng-controller="StudentController">
            <student name="Mahesh"></student><br/>
            <student name="Piyush"></student>
       </div>
    </body>
    </html>
参考URL:


函数{'use strict';var myApp=angular.module'myApp',[].controller'MyController',['$scope','http',函数$scope,$http{$scope.sortType=;$scope.sortReverse=true;$http.get.successfunction response{$scope.stories=response.data.children;}指令'myData',函数{return{templateUrl:'DataTable.html'};};这是我更改的控制器@anilhere是我的DataTable.html标题链接分数{{{x.data.Score}myapp=angular.modulemyyapp,[];myapp.directive'userinfo',函数{var directive={};directive.restrict='E';/*将此指令限制为元素*/directive.templateUrl=/myapp/html templates/div-template.html;return directive;};这是我的索引页现在看起来像简单的指令。templateUrl=DataTable.html;html是,这里userinfo是指令的名称这里是我的应用程序我想使用自定义指令显示数据,我现在用简单的html显示table@alecxe你能告诉我吗me@finspin你能告诉我怎么做吗这是我的应用程序代码