Bootstrap材料设计在动态角度视图下无法正常工作

时间:2014-10-05 19:25:24

标签: javascript angularjs twitter-bootstrap bower bootstrap-material-design

我将材料引导脚本包含在我的Angular项目的index.html中,但需要手动将它们重新包含在视图中才能工作。

这很奇怪,因为插入Angular的所有其他脚本都不会发生这种情况。

的index.html

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

我还注意到材料引导程序与Grunt和Bower不能很好地兼容,并且在构建时往往会自行删除(因此手册包含在页面底部)。

这些已知的错误有Material-boostrap和Angular / Bower / Grunt还是我做错了什么?

如果您需要其他任何信息,请告诉我们!

修改

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"
  }

1 个答案:

答案 0 :(得分:2)

问题在于bootstrap的材质设计插件通过将转换应用于DOM而工作,并且它不能用于像Angular这样的框架自动工作。

材料框架要求您声明类似以下内容的脚本来初始化组件:

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

这意味着对象是物化的&#34;在页面加载。由于Angular.js应用程序是单页面的,因此该样式仅应用于页面中已存在的元素(尝试添加新组件,也不使用视图:您应该得到相同的结果)。

如果在视图页面中包含脚本,则会获得所需的行为(功能不全),因为Angular.js在引擎盖下加载视图页面,因为它是普通页面,然后获取dom的内容和将视图树与单页树合并。

我建议您在加载视图后尝试添加对$ .material.init()的调用。 因为有涟漪库,所以多次调用init会有问题。如此处所述(https://github.com/FezVrasta/bootstrap-material-design/issues/184),您应该避免涟漪再次附加事件处理程序:

// 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;
}
相关问题