templateUrl在AngularJS中不起作用(templateUrl not working in AngularJS)

编程入门 行业动态 更新时间:2024-10-24 18:17:44
templateUrl在AngularJS中不起作用(templateUrl not working in AngularJS)

所以由于某种原因,我试图加载templateUrl的HTML页面没有显示出来。 这些文件都在同一个目录中,控制台显示没有错误,它只是不加载我想要添加的页面元素。 我的指令很简单:

.directive('tagTeste',function(){ return{ templateUrl: 'templateUrl.html' }; });

我的模板html我正在尝试加载:

<!doctype html> <html ng-app="teste"> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> </head> <body> <table border="1" width="500" height="150"> <thead> <tr> <td>Nome</td> <td>Telefone</td> </tr> </thead> <tbody> <tr ng-repeat="pessoa in pessoas"> <td>{{pessoa.nome}}</td> <td>{{pessoa.telefone}}</td> </tr> </tbody> </table> <form> <p> Nome: <input type="text" ng-model="nomenovo" required> </p> <p> Telefone: <input type="number" ng-model="numeronovo" required> </p> <input type="button" ng-click="add()"> </form> <body> </html>

我的索引页面:

<!doctype html> <html ng-app="teste"> <head> <h1 align="center">Table teste</h1> <meta charset="utf-8"> <title>Teste</title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script src="repeaterdirective.js"></script> <script type="text/ng-template" id="templateUrl.html"> </head> <div ng-controller="index" align="center"> <body> <div tag-teste></div> </body> </div> </html>

So for some reason the HTML page i'm trying to load with templateUrl is not showing up. The files are all in the same directory, console shows no error, it just doesn't load the page elements i'm trying to add. My directive is as simple as:

.directive('tagTeste',function(){ return{ templateUrl: 'templateUrl.html' }; });

My template html i'm trying to load:

<!doctype html> <html ng-app="teste"> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> </head> <body> <table border="1" width="500" height="150"> <thead> <tr> <td>Nome</td> <td>Telefone</td> </tr> </thead> <tbody> <tr ng-repeat="pessoa in pessoas"> <td>{{pessoa.nome}}</td> <td>{{pessoa.telefone}}</td> </tr> </tbody> </table> <form> <p> Nome: <input type="text" ng-model="nomenovo" required> </p> <p> Telefone: <input type="number" ng-model="numeronovo" required> </p> <input type="button" ng-click="add()"> </form> <body> </html>

My index page:

<!doctype html> <html ng-app="teste"> <head> <h1 align="center">Table teste</h1> <meta charset="utf-8"> <title>Teste</title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script src="repeaterdirective.js"></script> <script type="text/ng-template" id="templateUrl.html"> </head> <div ng-controller="index" align="center"> <body> <div tag-teste></div> </body> </div> </html>

最满意答案

从索引中删除<script type="text/ng-template" id="templateUrl.html"> 。

并清楚一点templateUrl.html 。 标签为<html>和<body>已包含在索引中。

看看plunker 。

评论后更新:

将指数更改为:

<!doctype html> <html ng-app="teste"> <head> <meta charset="utf-8"> <title>Teste</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script src="script.js"></script> </head> <body> <div ng-controller="index" align="center"> <h1 align="center">Table teste</h1> <div tag-teste></div> </div> </body> </html>

Remove <script type="text/ng-template" id="templateUrl.html"> from index.

And clear a bit templateUrl.html. Tags as <html> and <body> are already contained in index.

Take a look at plunker.

Updated after comment:

Change index as:

<!doctype html> <html ng-app="teste"> <head> <meta charset="utf-8"> <title>Teste</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script src="script.js"></script> </head> <body> <div ng-controller="index" align="center"> <h1 align="center">Table teste</h1> <div tag-teste></div> </div> </body> </html>

更多推荐

本文发布于:2023-07-24 19:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250174.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   templateUrl   AngularJS   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!