搜索文档
首页
HTML/CSS
JavaScript
服务端开发
Java教程
移动端
数据库
当前位置:
首页
JavaScript
AngularJS 教程
AngularJS 基础教程
源代码
清空
点击运行
<html> <head> <title>AngularJS-自定义指令</title> </head> <body> <h2>AngularJS-自定义指令示例</h2> <div ng-app = "mainApp" ng-controller = "StudentController"> <student name = "Sea"></student><br/> <student name = "Piyush"></student> </div> <script src = "https://cdn.staticfile.org/angular.js/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.Sea = {}; $scope.Sea.name = "Sea Gull"; $scope.Sea.rollno = 1; $scope.Piyush = {}; $scope.Piyush.name = "Piyush Gull"; $scope.Piyush.rollno = 2; }); </script> </body> </html>
运行结果