尝试执行 gulp serve
时收到以下错误消息在我的 AngularJS(1.6.10 版)应用上:
Error: [$injector:modulerr] Failed to instantiate module myAppName due to:
$compileProvider.preAssignBindingsEnabled is not a function
@http://localhost:9805/app/scripts/app.js:43:9
invoke@http://localhost:9805/lib/angular/angular.js:5108:16
runInvokeQueue@http://localhost:9805/lib/angular/angular.js:4997:11
loadModules/<@http://localhost:9805/lib/angular/angular.js:5007:11
forEach@http://localhost:9805/lib/angular/angular.js:387:11
loadModules@http://localhost:9805/lib/angular/angular.js:4987:5
createInjector@http://localhost:9805/lib/angular/angular.js:4904:19
doBootstrap@http://localhost:9805/lib/angular/angular.js:1936:20
bootstrap@http://localhost:9805/lib/angular/angular.js:1957:12
angularInit@http://localhost:9805/lib/angular/angular.js:1842:5
@http://localhost:9805/lib/angular/angular.js:35431:5
mightThrow@http://localhost:9805/lib/jquery/dist/jquery.js:3534:21
调用代码如下所示:
/*
* Main module of the application.
*/
angular
.module('dataPipelineApp', [
//various parameters
])
.config(['$compileProvider', '$httpProvider', '$breadcrumbProvider', function ($compileProvider, $httpProvider, $breadcrumbProvider) {
//initialize get if not there
if (!$httpProvider.defaults.headers.get) {
$httpProvider.defaults.headers.get = {};
}
$compileProvider.preAssignBindingsEnabled(true); //err here
$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
// extra
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
$compileProvider.debugInfoEnabled(false); // speed up angular performance to not print debug info;
$breadcrumbProvider.setOptions({
templateUrl: 'app/views/headerBar.html'
});
//$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
$httpProvider.interceptors.push("AddToken");
$httpProvider.interceptors.push("UnauthorizeInterceptor");
}])
类似的搜索似乎坚持认为这是一个版本差异。对此问题的类似搜索似乎也表明 angular-mocks 的版本存在问题,但是我们根本没有使用 angular mocks。我已经尝试将我的 Angular 降级到 1.5.5,其他搜索建议你不能超过 - 尽管我的所有同事都在 Angular 1.6.10 或更高版本上运行它。我也尝试使用 npm 安装 angular-mocks,尽管未使用,并同步版本以匹配我们的 Angular 版本,但无济于事。我真的不知道该怎么做,也不确定实际发生了什么,为什么它找不到该功能?
编辑:我还检查了浏览器,通过运行
angular.version
使用控制台进行搜索搜索 - 尽管我重做 gulp build
,但它仍然是 1.7.2 作为我的 Angular 版本和 gulp inject serve
在 npm 安装旧版本之后。似乎没有正确选择正确的版本 - 我是否缺少强制执行降级安装的内容?
最佳答案
$compileProvider.preAssignBindingsEnabled
标志在 AngularJS V1.6 中已弃用,并已从 AngularJS V1.7 中删除。
AngularJS 团队 强烈建议尽快迁移您的应用程序,不要依赖它。 AngularJS V1.6 于 2018 年 7 月 1 日停产。
从文档:
Due to 38f8c9, directive bindings are no longer available in the constructor.
Previously, the
$compileProvider.preAssignBindingsEnabled
flag was supported. The flag controlled whether bindings were available inside the controller constructor or only in the$onInit
hook. The bindings are now no longer available in the constructor.To migrate your code:
- If you specified
$compileProvider.preAssignBindingsEnabled(true)
you need to first migrate your code so that the flag can be flipped tofalse
. The instructions on how to do that are available in the "Migrating from 1.5 to 1.6" guide. Afterwards, remove the$compileProvider.preAssignBindingsEnabled(true)
statement.
从文档:
Due to bcd0d4, pre-assigning bindings on component/directive controller instances is disabled by default, which means that they will no longer be available inside the constructors. It is still possible to turn it back on, which should help during the migration. Pre-assigning bindings has been deprecated and will be removed in a future version, so we strongly recommend migrating your applications to not rely on it as soon as possible.
Initialization logic that relies on bindings being present should be put in the controller's
$onInit()
method, which is guaranteed to always be called after the bindings have been assigned.
笔记:
2018 年 7 月 1 日,终止了对 AngularJS 1.6 的支持。如需更多信息,请参阅 AngularJS MISC - Version Support Status .
关于AngularJS -$compileProvider.preAssignBindingsEnabled 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50937725/