简体   繁体   English

当包含使用ng-include时,ng-repeat不起作用

[英]ng-repeat doesnt work when include using ng-include

I am learning to code in Angular. 我正在学习Angular中的代码。 I am trying to perform a ng-repeat from within a file myTable.htm that is included in the main html file 1.html this however doesnt work. 我试图在主html文件1.html中包含的文件myTable.htm中执行ng-repeat但是这不起作用。

1.html 1.HTML

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>
<div ng-app=""> 
  <div ng-include="'myTable.htm'"></div>
</div>

<p>The HTML, and AngularJS code, for this table are located in the file "myTable.htm".</p>

</body>

</html>

myTable.htm myTable.htm

<div ng-app="myApp" ng-controller="myCtrl">

<select ng-model="selectedName" ng-options="x for x in names">
</select>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.names = ["Emil", "Tobias", "Linus"];
});
</script>
</div>

If I add <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> to myTable.htm and execute myTable.htm then I get the values in the select box. 如果我将<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>myTable.htm并执行myTable.htm然后我获取选择框中的值。 But it doesn't fill when I execute 1.html . 但是当我执行1.html时它没有填充。

Can anyone tell me what I have done wrong or whether what I am attempting to do is possible? 任何人都可以告诉我我做错了什么或者我试图做的是可能的吗?

You need to have the ng-app defined only in the first.html 您需要仅在first.html中定义ng-app

<div ng-app="myApp"> 

Remove it from the myTable.htm myTable.htm删除它

<div ng-controller="myCtrl">

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM