AngularJS-$ compileProvider.preAssignBindingsEnabled不是一个函数

时间:2018-06-19 22:24:33

标签: angularjs angularjs-1.6 angularjs-1.7

尝试在AngularJS(版本1.6.10)应用上执行gulp serve时收到以下错误消息:

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降级为1.5.5,但其他搜索结果却表明您不能超过Angular,尽管我的所有同事都在Angular 1.6.10或更高版本上运行它。我也尝试过使用npm来安装angular-mocks,尽管它没有被使用,并且同步了版本以匹配我们的Angular的版本,但是无济于事。我真的不确定该怎么做,也不确定实际正在发生什么,为什么它找不到该功能?

编辑:我还检查了浏览器,通过运行angular.version搜索来使用控制台进行搜索-尽管我重做了gulp build和{{1 }}在安装较早版本的npm之后。似乎没有正确选择正确的版本-强制降级安装是否缺少我的东西?

2 个答案:

答案 0 :(得分:3)

$compileProvider.preAssignBindingsEnabled标志已在AngularJS V1.6中弃用,并已从AngularJS V1.7中删除。

AngularJS团队强烈建议迁移您的应用程序,使其不尽快依赖它。 AngularJS V1.6于2018年7月1日终止生命。

从文档中:

  

由于38f8c9,指令绑定在构造函数中不再可用。

     

以前,支持$compileProvider.preAssignBindingsEnabled标志。该标志控制绑定是在控制器构造函数内部可用还是仅在$onInit挂钩中可用。现在,绑定不再在构造函数中可用。

     

要迁移代码,请执行以下操作:

     
      
  • 如果您指定了$compileProvider.preAssignBindingsEnabled(true),则需要首先迁移代码,以便将标志翻转到false"Migrating from 1.5 to 1.6" guide中提供了有关操作方法的说明。然后,删除$compileProvider.preAssignBindingsEnabled(true)语句。
  •   
     

— AngularJS Developer Guide - Migrating to V1.7 - Compile

从文档中:

  

由于bcd0d4,默认情况下禁用组件/指令控制器实例上的预分配绑定,这意味着它们将不再在构造函数中可用。仍然可以重新打开它,这在迁移过程中会有所帮助。预分配绑定已被弃用,并将在以后的版本中删除,因此我们强烈建议迁移您的应用程序以使其不尽快依赖它。

     

依赖于存在的绑定的初始化逻辑应放在控制器的$onInit()方法中,该方法保证在分配绑定后总是调用该方法。

     

— AngularJS Developer Guide - Migrating to 1.6 - Compile

注意:

2018年7月1日,对AngularJS 1.6的支持终止。有关更多信息,请参见AngularJS MISC - Version Support Status

答案 1 :(得分:0)

自AngularJS 1.6起不再支持它。对于仍在将其应用程序迁移到较新版本的AngularJs的人们,如果您指定$compileProvider.preAssignBindingsEnabled(true),则需要 首先迁移代码,以便将标志翻转到false。并查看本指南https://docs.angularjs.org/guide/migration#migrating-from-1-5-to-1-6

AngularJs提交参考:https://github.com/angular/angular.js/commit/38f8c97af74649ce224b6dd45f433cc665acfbfb