Alan Alan - 5 months ago 15
HTML Question

only one of two css files is in effect in angular 2 app

I am following a tutorial on angular 2, and at some point i had to use bootstrap classes. In the external libraries folder, bootstrap exists but it didn't seem to work. To solve that i imported it in the

index.html
file, and although it now works, my other css file doesn't. These are the contents of
index.html
(css files in lines 6-7):

<html>
<head>
<title>Angular 2 QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>


also here is the part where i use the bootstrap classes:

template: `
<h1>Hello</h1>
<i class="glyphicon"
[class.glyphicon-star] = "isFull"
[class.glyphicon-star-empty] = "!isFull"
(click)="onClick()">
</i>
`


and finally
styles.css
:

h1 {
color: #369;
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}
body {
margin: 2em;
}

Answer

You must have heard about ViewEncapsulation metadata property of @Component. You can use it like this way for bootstrap to work with your custom style,

@Component({
    selector: 'my-app',
    encapsulation: ViewEncapsulation.none,
    styleUrls:  ['style.css'],
    ...
    ...
})

Check working demo,

http://plnkr.co/edit/oQYg5EFgaxoWdYyD8yhl?p=preview

Comments