Exobros Exobros - 2 months ago 14
AngularJS Question

Adding ng-href to a button in angularjs changes the position of the text inside the button

I'm starting to learn Angularjs (using angular-material), and I have a little problem using ng-href.
I made a Toolbar at the top of my web page, but as soon as I add the "ng-href" attribute to a button, the Text inside the Button isn't centered anymore:

Example Image

The first 2 Buttons have the ng-href tag added. The third one hasn't. Otherwise, they are exactly the same.



angular.module('angular2',['ngRoute','ngMaterial','ngMessages'])
.config(function ($mdThemingProvider,$routeProvider) {
$mdThemingProvider.theme('default')
.primaryPalette('blue')
.accentPalette('blue');
$routeProvider
.when("/", {
templateUrl : "main.html"
})
.when("/test1", {
templateUrl : "test1.html"
})
.when("/test2", {
templateUrl : "test2.html"
})
})
.controller('mainCtrl',function ($scope, $http, $mdDialog) {

})
;

.navButton{
font-weight: bold;
margin: 0px;
height: inherit;
width: 150px;
border-radius: 0px;
text-transform: none;
border: solid;
}

<!DOCTYPE html>
<html lang="en" ng-app="angular2">
<head>
<meta charset="UTF-8">
<title>Angular 2</title>

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic">
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="app.css">


<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>

<script src="app.js"></script>
</head>

<body ng-controller="mainCtrl">
<div layout="column">
<md-toolbar class="md-menu-toolbar" style="height: 64px">
<div layout="row" style="height: inherit">
<md-toolbar-filler flex="5" style="height: inherit" layout layout-align="center center">
<md-icon class="material-icons md" style="font-size: 48px; height: 48px; width: 48px">mail_outline
</md-icon>
</md-toolbar-filler>
<div flex layout="row" layout-align="start center" style="height: inherit">
<md-button class="md-primary navButton" ng-href="#/">Main</md-button>
<md-button class="md-primary navButton" ng-href="#/test1">Test 1</md-button>
<md-button class="md-primary navButton">Test 2</md-button>
</div>
</div>
</md-toolbar>
<div ng-view></div>
</div>


</body>

</html>





Somehow I couldn't manage to center the Text inside the Buttons after I added the ng-href attribute.

Answer

I couldn't get a fiddle to recreate this. But I played with the md-button directive and normally it works fine for this. There is potentially something in your app.css that is conflicting.

When md-button sees an href or ng-href, it will create an tag and put your button content into a . So your

<md-button class="md-primary navButton" ng-href="#/test1">Test 1</md-button>

becomes

<a class="md-primary navButton md-button md-ink-ripple" 
        ng-transclude="" ng-href="#/test1" href="#/test1">
    <span class="ng-scope">Test 1</span>
</a>

angular-material.css gives that tag a text-align:center, so check your CSS for something that is changing the attributes of the when it's within .

Here's the Fiddle I was working with but isn't exactly the same as yours: https://jsfiddle.net/phanxo14/

Comments