最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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=".1.0/angular-material.min.css">
    <link rel="stylesheet" href=":300,400,500,700,400italic">
    <link rel="stylesheet" href=".0.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="+Icons">
    <link rel="stylesheet" href="app.css">


    <script src=".5.5/angular.min.js"></script>
    <script src=".5.5/angular-animate.min.js"></script>
    <script src=".5.5/angular-aria.min.js"></script>
    <script src=".5.5/angular-messages.min.js"></script>
    <script src=".4.8/angular-route.js"></script>
    <script src=".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>

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


    <script src="http://ajax.googleapis./ajax/libs/angularjs/1.5.5/angular.min.js"></script>
    <script src="http://ajax.googleapis./ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
    <script src="http://ajax.googleapis./ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
    <script src="http://ajax.googleapis./ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
    <script src="http://ajax.googleapis./ajax/libs/angularjs/1.4.8/angular-route.js"></script>
    <script src="http://ajax.googleapis./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.

Share Improve this question edited Sep 22, 2016 at 13:50 StephenTG 2,6676 gold badges28 silver badges37 bronze badges asked Sep 22, 2016 at 13:47 ExobrosExobros 231 gold badge1 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 0

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 <a> tag and put your button content into a <span>. So your

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

bees

<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 <a> tag a text-align:center, so check your CSS for something that is changing the attributes of the <span> when it's within <a>.

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

I kind of managed to fix the Problem: Instead of using the md-button, that anuglarjs later turned into an <a> tag I copied the <a> tag that angular generated instead and used that one directly:

<a class="md-primary navButton md-button md-ink-ripple" ng-transclude="" ng-href="#/" style="text-align: center; height:inherit" href="#/" layout>
    <span class="ng-scope" flex style="text-align: center">Main</span>
</a>

Then I added the "layout" attribute to the surrounding <a> tag and the "flex" attribute to the inner <span>. I acutally have no idea why this works, but the Text is now centered.

Edit: A Better and much easier fix was actually to just add the layout and layout-align="center center" attributes to the button.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far