创建图标按钮
此示例将使用类 md-icon-button
,必须将其应用于 <md-button>
才能获得图标按钮。
- 为了方便起见,还建议为
<md-button>
添加aria-label
属性,否则ARIA provider
会发出警告,表示没有aria-label
。 - 通常,
<md-button>
属性中有<md-icon>
元素。 - 可选地,还可以存在
<md-tooltip>
元素以提供按钮的工具提示。
此示例将使用 Google 的 Material Icons 。
index.html :
<html ng-app="mdIconButtonApp">
<head>
<!-- Import Angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.11/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.11/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.11/angular-aria.min.js"></script>
<!-- Angular Material -->
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.5.11/angular-material.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.4/angular-material.min.css" rel="stylesheet">
<!-- Material Icons -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<!-- Roboto -->
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<!-- app.js -->
<script src="app.js"></script>
<style>
body {
font-family: Roboto, sans-serif;
}
</style>
</head>
<body>
<md-content ng-controller="mdIconButtonController">
<!--
Normal `md-button`
Note that it is recommended to add a `aria-label` to `md-icon-button` for accessibility purposes.
-->
<md-button class="md-icon-button" aria-label="{{ariaLabel}}">
<md-icon class="material-icons">menu</md-icon>
</md-button>
<md-button class="md-primary md-icon-button" aria-label="{{ariaLabel}}">
<md-icon class="material-icons">menu</md-icon>
<md-tooltip>This is a tooltip!</md-tooltip>
</md-button>
<md-button class="md-accent md-icon-button md-fab" ng-click="goToLink('https://github.com/android')" aria-label="Go To Android">
<md-icon class="material-icons">android</md-icon>
</md-button>
<md-button class="md-warn md-icon-button" ng-href="{{link}}" aria-label="{{ariaLabel}}">
<md-icon class="material-icons">{{icon}}</md-icon>
</md-button>
<md-button class="md-raised md-icon-button" ng-click="goToLink('http://example.com')">
<!-- Note that you must have $mdIconProvider for this -->
<md-icon md-svg-icon="link"></md-icon>
</md-button>
<md-button class="md-cornered md-primary md-hue-1 md-icon-button" aria-label="{{ariaLabel}}">
<!-- You can also use the source of SVG -->
<md-icon md-svg-src="/path/to/more.svg"></md-icon>
</md-button>
<md-button class="md-accent md-hue-2" aria-label="{{ariaLabel}}">
<md-icon class="material-icons">g-translate</md-icon>
</md-button>
<md-button class="md-warn md-hue-3 md-icon-button" ng-href="{{link}}" aria-label="Link">
<md-icon md-svg-icon="copyright"></md-icon>
</md-button>
</md-content>
</body>
</html>
app.js :
angular.module('mdIconButtonApp', ['ngAnimate', 'ngAria', 'ngMaterial', 'ngMessages'])
.config(function($mdIconProvider) {
// Configure iconsets: More info: https://material.angularjs.org/latest/api/service/$mdIconProvider
$mdIconProvider.iconSet('/path/to/media-icons.svg')
.defaultIconSet('/path/to/icons.svg')
.icon('sample-icon', '/path/to/sample-icon.svg');
})
.controller('mdIconButtonController', function($scope) {
$scope.ariaLabel = "Button";
$scope.icon = "menu";
$scope.link = "https://google.com";
$scope.goToLink = function(link) {
// Go to some site
console.log('Redirecting to:' + link);
window.location.href=link;
}
})