Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap material design not working properly with dynamic angular views

I'm including material-bootstrap scripts into the index.html of my Angular project, but they need to be manually re-included in the views to work.

This is strange, as for all other scripts inserted into Angular this does not happen.

index.html

<script src="bower_components/bootstrap-material-design/scripts/material.js"></script>
<script src="bower_components/bootstrap-material-design/scripts/ripples.js"></script>

I also noticed that material-bootstrap doesn't play well with Grunt and Bower, and tends to remove itself on build (hence the manual includes at the bottom of the page).

Are these known bugs with Material-boostrap and Angular/Bower/Grunt or have I been doing something wrong?

If you require anything else please let me know!

edit:

dependencies in bower.json

"dependencies": {
    "angular": "~1.3.0",
    "json3": "~3.3.1",
    "es5-shim": "~3.1.0",
    "bootstrap": "~3.2.0",
    "angular-resource": "~1.3.0",
    "angular-cookies": "~1.3.0",
    "angular-sanitize": "~1.3.0",
    "angular-animate": "~1.3.0",
    "angular-touch": "~1.3.0",
    "angular-route": "~1.3.0",
    "bootstrap-material-design": "*",
    "jsjws": "~3.0.2",
    "angular-ui-router": "0.2.11"
  }
like image 397
Phil Hudson Avatar asked Oct 05 '14 19:10

Phil Hudson


People also ask

Can I mix Bootstrap and angular material together?

Yes, you can use parts of Angular Material and Bootstrap together in the same web or mobile project. Developers need to be careful not to use the same components, which can clash. Angular Material and Bootstrap offer a variety of unique components for great website design.

Should I use Bootstrap or material with angular?

Angular Material Framework can be used to design High-quality websites and web apps that are mobile-friendly. Bootstrap Framework can be used for developing cross-platform apps that work well on desktops. Angular Material Framework does not support the Responsive Oriented Approach (ROA).

Can we use material design with Bootstrap?

Yes, material design makes this possible with Bootstrap (also known as mdbootstrap). It is a set of libraries built on Bootstrap and follows other well-known frameworks and content design guidelines such as Vue, Angular, React.

Is angular material mobile-friendly?

The main aim of Angular Material is to enable the developers to offer the best Material UI/UX design to the end-users. It is a mobile-friendly and responsive technology that helps in creating attractive web pages and applications by following modern approaches.


1 Answers

The problem is that the material design plugin for bootstrap works by applying transformations to the DOM and it is not designed to work automatically with frameworks like Angular.

The material framework requires you to declare a script like the following to initialize components:

<script>
  $.material.init();
</script>

This means that object are "material-ized" at page loading. Since an Angular.js application is single-paged, the style is applied only to elements already present in the page (try to add new component, also without using views: you should get the same result).

You get the required behaviour (not fully functional) if you include the scripts in the views pages because Angular.js, under the hood, loads the view page as it was a normal page, then takes the contents of the dom and merge the view tree with the single-page tree.

I suggest you trying to add a call to $.material.init() after loading the view. Be careful as there are issues with calling the init multiple times, because of the ripples library. As stated here (https://github.com/FezVrasta/bootstrap-material-design/issues/184) you should avoid ripples to attach the event handlers again:

// add the "done" boolean inside ripples.js
window.ripples = { 
    init: function(withRipple) { /*bla bla*/ },
    done: false
}

// Then, where you run ripples.init... (aka, material.js, or maybe directly inside the init function of ripples.js)
if (!window.ripples.done) { 
  ripples.init();
  ripples.done = true;
}
like image 132
Nicola Ferraro Avatar answered Sep 29 '22 02:09

Nicola Ferraro