我正在尝试在其中包含一个HTML代码段ng-repeat,但我无法让include工作.似乎当前的语法与ng-include以前的语法不同:我看到许多例子使用
<div ng-include src="path/file.html"></div>
Run Code Online (Sandbox Code Playgroud)
但在官方文档中,它表示要使用
<div ng-include="path/file.html"></div>
Run Code Online (Sandbox Code Playgroud)
但是然后在页面上显示为
<div ng-include src="path/file.html"></div>
Run Code Online (Sandbox Code Playgroud)
无论如何,我试过了
<div ng-include="views/sidepanel.html"></div>
Run Code Online (Sandbox Code Playgroud)
<div ng-include src="views/sidepanel.html"></div>
Run Code Online (Sandbox Code Playgroud)
<ng-include src="views/sidepanel.html"></ng-include>
Run Code Online (Sandbox Code Playgroud)
<ng-include="views/sidepanel.html"></ng-include>
Run Code Online (Sandbox Code Playgroud)
<ng:include src="views/sidepanel.html"></ng:include>
Run Code Online (Sandbox Code Playgroud)
我的代码片段不是很多代码,但是它有很多内容; 我在里面ng-repeat读动态加载模板可能会导致问题,所以我用sidepanel.html单词替换了内容foo,但仍然没有.
我也尝试直接在页面中声明模板,如下所示:
<script type="text/ng-template" id="tmpl">
foo
</script>
Run Code Online (Sandbox Code Playgroud)
并贯穿ng-include引用脚本的所有变体,但id仍然没有.
我的页面中有更多内容,但现在我已将其删除到这个:
<!-- index.html -->
<html>
<head>
<!-- angular includes -->
</head>
<body ng-view="views/main.html"> <!-- view is actually set in the router -->
<!-- views/main.html -->
<header>
<h2>Blah</h2>
</header> …Run Code Online (Sandbox Code Playgroud) 我不太明白何时使用指令以及何时使用nginclude更合适.举个例子:我有一个部分,password-and-confirm-input-fields.html就是用于输入和确认密码的html.我在注册页面和更改密码页面下使用它.这两个页面各有一个控制器,部分html没有专用控制器.
我应该使用指令还是ngInclude为此?
我有一个控制器,我写的,我在多个地方使用我的应用程序ng-include和ng-repeat,就像这样:
<div
ng-repeat="item in items"
ng-include="'item.html'"
ng-controller="ItemController"
></div>
Run Code Online (Sandbox Code Playgroud)
在控制器/模板中,我希望item价值存在,而整个事物都围绕着这个想法.但是,现在,我需要以稍微不同的方式使用控制器,没有ng-repeat,但仍然需要能够传入item.我看到ng-init并认为它可以做我需要的,像这样:
<div
ng-init="item = leftItem"
ng-include="'item.html'"
ng-controller="ItemController"
></div>
<div
ng-init="item = rightItem"
ng-include="'item.html'"
ng-controller="ItemController"
></div>
Run Code Online (Sandbox Code Playgroud)
但这似乎并没有奏效.任何人都有任何想法如何在像这样的单一实例中传入变量的范围?
编辑:上面的控制器正在加载leftItem和rightItem值,如下所示:
.controller('MainController', function($scope, ItemModel) {
ItemModel.loadItems()
.then(function(items) {
$scope.$apply(function() {
$scope.leftItem = items.left;
$scope.rightItem = items.right;
});
});
});
Run Code Online (Sandbox Code Playgroud) javascript angularjs angularjs-ng-repeat angularjs-ng-include angularjs-ng-init