AngularJS自定义指令实现面包屑功能完整实例

<!DOCTYPE html> <html lang="zh-CN" ng-app="myApp"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta content="width=device-width, initial-scale=1"> <script src="https://www.jb51.net/jquery.min.js"></script> <script src="https://www.jb51.net/angular.js"></script> <script src="https://www.jb51.net/bootstrap.min.js"></script> <link href="https://www.jb51.net/bootstrap.min.css" > <script type="text/javascript"> var myApp = angular.module('myApp', []); myApp.controller('Ctrl', function($scope){ $scope.crumbOptions = [ {"href": "http://www.baidu.com", "title" : "Home"}, {"href": "http://www.sina.com", "title" : "Library"}, {"href": "", "title" : "Data"} ]; }); myApp.directive("custBreadcrumb", function() { return { restrict: 'E', replace: true, scope: { options:'@' }, link: function(scope, elem, attrs) { var parentNode = elem.parent(); var crumbString = '<ol>'; angular.forEach(scope.$eval(scope.options), function(item) { if (item["href"] != "") { var tempString = '<li><a href="' + item[" href"] + '">' + item["title"] + '</a></li>'; crumbString += tempString; } else { var tempString = '<li>' + item["title"] + '</li>'; crumbString += tempString; } }); crumbString += "</ol>"; parentNode.append(crumbString); } }; }); </script> </head> <body ng-controller="Ctrl"> <cust-breadcrumb options="{{crumbOptions}}"></cust-breadcrumb> </body> </html>

更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wysjgz.html