角度 UI/引导类型提前显示“错误:无控制器:ngModel”错误

Angular UI/bootstrap typeahead showing 'Error: No controller: ngModel' error

本文关键字:错误 控制器 ngModel 显示 UI 类型 角度      更新时间:2023-09-26

在我的应用程序中,我调用了一个http服务来获取数据,并且我正在使用angular-ui bootstrap的typeahead指令(ui-bootstrap-tpls-0.6.0.min.js)。我有一个部分,它有一个提到控制器的形式,它包括一个部分内部的 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

学院类型.html:

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

教育学院Ctrl.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;
        }); 
    };
   ...

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

控制台.log:

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

我对错误的理解是指令中缺少"必需"属性,这就是角度所抱怨的,但正如您在部分中看到的那样,我确实定义了 ng-model 属性,并且控制器也以主形式部分指定。我在这里错过了什么?

编辑:删除了网址的不相关部分。

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