如何将控制器放在外部HTML文件中 AngularJS.
how to put controller in external html file angularjs
我对AngularJS很陌生。
我有一个带有一些按钮的网络应用程序:
索引.html
<button class="aButton">a</button>
<button class="bButton">b</button>
<script>
$(document).ready(function(){
$(".aButton").click(function(){
$(".aField").fadeIn(400);
});
});
</script>
<script>
$(document).ready(function(){
$(".bButton").click(function(){
$(".bField").fadeIn(400);
});
});
</script>
当我单击不同的按钮时,它们会根据单击的按钮显示 iframe。在这些 iframe 中,我通过 src 放置了一个外部 html 文件。
<iframe class="aField" style="display: none;" src="a.html" width="700" height="1000" ></iframe>
<iframe class="bField" style="display: none;" src="b.html" width="700" height="1000" ></iframe>
直到这里没问题。
问题出在外部 html 文件(a.html 和 b.html)中,每个文件都需要不同的控制器。我尝试将 ng-controller 标签放在 iframe 和外部 html 文件中,但我在控制器中定义的函数不起作用。
知道吗?如果我不清楚,请告诉我。谢谢。
如果你正在加载iframe,那么内容将不知道任何关于角度的信息,所以控制器在外部HTML中不起作用。
您可能需要查看将部分 HTML 文件包含在页面中的ng-include
。使用它,HTML将直接插入到您的页面中,并像应用程序的其余部分一样进行编译/链接。
下面是使用 ng-include
的示例。我还用ng-click
替换了jQuery点击处理程序,用ng-show
替换了.fadeIn
s,以使解决方案更有棱角。
<!-- These buttons just set variables on the scope when clicked -->
<button ng-click="showA=true">a</button>
<button ng-click="showB=true">b</button>
<script>
// Nothing left here now
// The contents of ng-click could be moved into a JS controller here...
</script>
<!-- the ng-include attribute value is an angular expression. If you use a string, wrap it in quotes -->
<div ng-include="'a.html'" ng-show="showA" width="700" height="1000"></div>
<div ng-include="'b.html'" ng-show="showB" width="700" height="1000" ></div>
相关文章:
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 导入jQuery脚本获胜'我不处理html文件
- 生成pdf或其他非html文件时的错误处理
- 如果使用javascript函数屏幕太小,我该如何更改HTML文件的背景色
- html文件中的script标记根本不起作用
- 从单独的html文件预览Javascript文本
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- Android在webview中加载带有javascript的html文件,得到Uncaught ReferenceEr
- 什么更好?使用iframe或jQuery之类的东西在外部网站中加载HTML文件
- 当我在浏览器中打开HTML文件时,javascript不起作用
- 如何在另一个html文件的框架中包含图像
- 压缩静态HTML文件中的JS和CSS
- 从HTML文件中的TypeScript(.ts)获取值
- 以HTML文件中的编码形式脱机存储图像
- ionic幻灯片框中的每张幻灯片都有不同的HTML文件
- 加载具有特定CSS的HTML文件
- 使用Gulp手柄部分编译为单个HTML文件
- 使用javascript检查服务器上是否存在html文件
- 在本地启动HTML文件
- javascript从选中的复选框中检索输入值,并在同一网页中显示多个html文件