<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>封装一个面包屑导航</title>
<!-- 引入angular的库 -->
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
<script src="bower_components/angular/angular.js"></script>
</head>
<!-- ng-app用来声明 angular 执行的边界
ng-controller 用来声明 我们要调用哪一个 controller
-->
<body ng-app="myAppDemo" ng-controller="myAppController">
<!-- 这是我们自定义的指令 -->
<breadcrumb data='{{pathData}}'></breadcrumb>
<script>
//声明我们的模块 myApp
let myApp = angular.module('myAppDemo', []);
/*在模块写创建一个控制器 myAppController*/
myApp.controller('myAppController', ['$scope', function($scope){
/*定义一个数组*/
$scope.pathData = {
first:'#',
second:'#',
third:'#',
fourth:'#',
};
}]);
//定义一个面包屑导航指令
myApp.directive('breadcrumb', [function(){
/*返回指令对象*/
return{
scope:{},
templateUrl:'temps/breadcrumb.html',
replace:true,
link:function(scope, element, attributes){
scope.data = JSON.parse(attributes.data);
console.log(scope.data);
}
};
}]);
</script>
</body>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>封装一个面包屑导航</title>
<!-- 引入angular的库 -->
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
<script src="bower_components/angular/angular.js"></script>
</head>
<!-- ng-app用来声明 angular 执行的边界
ng-controller 用来声明 我们要调用哪一个 controller
-->
<body ng-app="myAppDemo" ng-controller="myAppController">
<!-- 这是我们自定义的指令 -->
<breadcrumb data='{{pathData}}'></breadcrumb>
<script>
//声明我们的模块 myApp
let myApp = angular.module('myAppDemo', []);
/*在模块写创建一个控制器 myAppController*/
myApp.controller('myAppController', ['$scope', function($scope){
/*定义一个数组*/
$scope.pathData = {
first:'#',
second:'#',
third:'#',
fourth:'#',
};
}]);
//定义一个面包屑导航指令
myApp.directive('breadcrumb', [function(){
/*返回指令对象*/
return{
scope:{},
templateUrl:'temps/breadcrumb.html',
replace:true,
link:function(scope, element, attributes){
scope.data = JSON.parse(attributes.data);
console.log(scope.data);
}
};
}]);
</script>
</body>
</html>
/**
breadcrumb.html文件
*/
<ol class="breadcrumb">
<li ng-repeat="(key, value) in data track by $index">
<a href="{{value}}" ng-class="{{active:$last}}"
ng-if="!$last">{{key}}</a>
<span ng-if="$last"></span>
</li>
</ol>