gpt4 book ai didi

javascript - Angular UI/bootstrap typeahead 显示 'Error: No controller: ngModel' 错误

转载 作者:搜寻专家 更新时间:2023-11-01 04:41:54 25 4
gpt4 key购买 nike

在我的应用程序中,我正在调用一个 http 服务来获取数据,并且我正在使用 angular-ui bootstrap 的 typeahead 指令 (ui-bootstrap-tpls-0.6.0.min.js)。我有一个部分,它有一个提到 Controller 的形式,它在 ng-repeat 中包含一个部分。第二部分有提前输入。

主要形式部分:

<form
id="myform"
name="myform"
onsubmit="javascript: return false"
enctype="application/json"
ng-controller="EducationCollegeCtrl">
// doing other stuff
...

<div ng-if="model.hasData">
<div ng-repeat="college in model.academicRecords" ng-form="collegeForm">
<div ng-include="'resources/appc/modules/main/education/components/collegetype.all.html'"></div>
</div>
</div>
// other stuff going on here

collegetype.all.html:

....
<label for="institution">Institution name:</label>
<div>
<input type="text" ng-model="college.organizationName" typeahead="item.name for item in matchingInstitutions($viewValue)>
</div>
....

EducationCollegeCtrl.js:

angular.module('theApp',['ui.bootstrap']).controller('EducationCollegeCtrl', function ($scope, $http) {
...
$scope.matchingInstitutions = function(partialName) {
return $http.get('lookup/institutions?name=' + partialName ).then(function(response){
return response.data.institutions;
});
};

...

服务被调用,下拉列表正确显示机构名称。但是在浏览器控制台中,我看到下拉列表中的每个条目都出现以下错误

控制台日志:

Error: No controller: ngModel
at Error (<anonymous>)
at getControllers (/resources/lib/angular/1.1.5/angular.js:4899:39)
at nodeLinkFn (/resources/lib/angular/1.1.5/angular.js:5040:55)
at compositeLinkFn (/resources/lib/angular/1.1.5/angular.js:4626:37)
at nodeLinkFn (/resources/lib/angular/1.1.5/angular.js:5033:40)
at compositeLinkFn (/resources/lib/angular/1.1.5/angular.js:4626:37)
at publicLinkFn (/resources/lib/angular/1.1.5/angular.js:4531:46)
at ngRepeatAction (/resources/lib/angular/1.1.5/angular.js:15638:33)
at Object.$watchCollectionAction (/resources/lib/angular/1.1.5/angular.js:8867:29)
at Object.applyFunction [as fn] (<anonymous>:778:50) <typeahead-match index="$index" match="match" query="query" template-url="templateUrl" class="ng-isolate-scope ng-scope"> angular.js:6422

我对错误的理解是指令中缺少一个“必需”属性,这就是 Angular 所提示的,但是正如您在部分中看到的那样,我确实定义了 ng-model 属性以及 Controller 在主窗体部分指定。我在这里错过了什么?

编辑:删除了 url 中不相关的部分。

最佳答案

ng-include 创建子 $scope,其原型(prototype)继承其父 $scope。这意味着您的 ng-model="college.organizationName" 不会在父级中引用 college 变量,但会隐藏它。并且您的新 college 变量将没有 organizationName 属性。最简单的方法可能就是不使用 ng-include,因为您有 ng-repeat 并且已经避免了“不要重复自己”规则。

关于javascript - Angular UI/bootstrap typeahead 显示 'Error: No controller: ngModel' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19145073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com