简体   繁体   中英

How to apply css in angular js using ng-class

I am new to angular js. I want to apply css styles from external css file. i have defined .container class in css file i want to call it in angular js file using ng-class. But it is not working y. style.css

.container
{
width:900px;
height:600px;
border:1px solid;
margin:auto 0;
position:absolute;
background:red;
}

index.html

    <!DOCTYPE html>
    <html>
    <head>
    <script src="js/Angular.js"></script>
    <link rel="stylesheet" href="css/style.css"  />
    </head> 
    <body>
    <div ng-app="" ng-class="container" >

    </div>
    </body>
    </html>

you can use:

ng-class="{'container':true}"

see ngClass

To apply a class like that, you can simply use class :

<div ng-app="" class="container" >
               ^^^^^

From the docs :

The ngClass directive allows you to dynamically set CSS classes on an HTML element by databinding an expression that represents all classes to be added.

If you still want to use ng-class :

<div ng-app="" ng-class="{'container':true}" >

An example fiddle.

This is no ng-class, but a regular class. the ng-class directive is to do more than just assigning a class, but for example assign the class through a condition. If you simply do like this:

<div ng-app="" class="container">

is sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM