【发布时间】:2013-07-12 20:45:30
【问题描述】:
我在我的应用程序的“索引”页面上使用AngularUI Typeahead。我没有做任何花哨的事情 - 事实上,我只是想让他们在他们的 UI 网站上建立的示例正常工作,我收到了这个错误:
Error: Template must have exactly one root element
我不知道这是什么意思,但只有当我有以下代码时才会发生:
<input type="text" ng-model="selected" typeahead="state for state in states | filter:$viewValue">
如果相关,我的主页控制器(通过 $routeProvider 调用 / 索引目录):
function indexCtrl($scope, $location, $resource) {
$scope.selected = undefined;
$scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
}
请注意,页面上的其他控制器工作正常,只是 $scope.selected/$scope.states 引起了麻烦。我无法弄清楚错误的含义,因此故障排除非常困难!
有什么想法吗?
编辑这是我的 HTML 模板:
<html ng-app="myApp" class="ng-scope">
<head>
// Head stuff, probably irrelevant
</head>
<body>
<div class="container">
<div ng-view="" class="row-fluid">
<form class="row-fluid">
<div class="container-fluid">
<input type="text" ng-model="selected" typeahead="state for state in states | filter:$viewValue">
</div>
</form>
</div>
</div>
// A bunch of Angular scripts are here
</body>
</html>
这绝对与 typeahead 脚本有关,我可以删除 typeahead="state for state in states | filter:$viewValue" 并且脚本可以工作(尽管显然 typeahead 函数不...)
【问题讨论】:
-
input(type="text", ng-model="selected", typeahead="state for state in states | filter:$viewValue")是什么,不是<input type="text" ...>吗? -
@YeLiu 哦,抱歉,这是 ExpressJS 使用的
Jade模板引擎的一部分。它就像你说的那样呈现,我会更新问题以反映这一点。 -
发布您的 html/模板,因为错误与模板有关。
-
@YeLiu 我不明白在这种情况下“根元素”是什么意思。编辑以包含我的模板。
标签: javascript angularjs angular-ui angular-ui-bootstrap