【问题标题】:Angular 1.5 components with ui-router resolve : Unknown provider具有 ui-router 解析的 Angular 1.5 组件:未知提供者
【发布时间】:2016-11-15 17:47:48
【问题描述】:

我在将控制器转换为组件为 Angular 2 准备我的应用程序时遇到了问题,但问题是迁移并不顺利,我有 ui-router 在状态之间路由并在一些控制器中使用 resolve,带有控制器的版本正在工作,但组件的版本现在完全可以工作,我遵循了很多指南,似乎我在代码方面做得很好,但对我不起作用。

我有以下带有控制器模块

(function () {
  'use strict';

  angular
    .module('app.sample', [])
    .config(config);

  /** @ngInject */
  $stateProvider
    .state('app.sample', {
      url    : '/sample',
      views  : {
        'content@app': {
          templateUrl: 'app/main/sample/sample.html',
            controller : 'SampleController as vm'
          }
        },
        resolve: {
          SampleData: function (myService) {
            return myService.getSample(); // I return a promise here
          }
       }
     });
  }
})();

控制器

(function ()
{
    'use strict';
    angular
        .module('app.sample')
        .controller('SampleController', SampleController);

    /** @ngInject */
    function SampleController(SampleData)
    {
        var vm = this;
        vm.helloText = SampleData.data.helloText;
    }
})();

上面的代码运行良好,但是将它做成一个组件后,它变成了这样:

(function () {
  'use strict';

  angular
    .module('app.sample', [])
    .config(config);

  /** @ngInject */
  function config($stateProvider) {
    // State
    $stateProvider
      .state('app.sample', {
        url: '/sample',
        views: {
          'content@app': {
            template: '<sample></sample>'
          }
        },
        resolve: {
          SampleData: function (myService) {
            return myService.getSample(); // I return a promise here
          }
        }
      });
  }
})();

组件

(function () {
  'use strict';

  angular
    .module('app.sample')
    .component('sample', {
      templateUrl: 'app/main/sample/sample.html',
      bindings: {
      },
      controller: Sample
    });

  /** @ngInject */
  function Sample(SampleData) {
    var $ctrl = this;
    $ctrl.helloText = SampleData.data.helloText;
  }
})();

但现在它不起作用并给我以下错误:

Error: [$injector:unpr] Unknown provider: SampleDataProvider <- SampleData
http://errors.angularjs.org/1.5.7/$injector/unpr?p0=SampleDataProvider%20%3C-%20SampleData
    at angular.js:68
    at angular.js:4502
    at Object.getService [as get] (angular.js:4655)
    at angular.js:4507
    at getService (angular.js:4655)
    at injectionArgs (angular.js:4679)
    at Object.invoke (angular.js:4701)
    at $controllerInit (angular.js:10234)
    at nodeLinkFn (angular.js:9147)
    at angular.js:9553

我在 bower.json 中的依赖项:

"dependencies": {
    "angular": "1.5.7",
    "angular-animate": "1.5.7",
    "angular-aria": "1.5.7",
    "angular-cookies": "1.5.7",
    "angular-material": "1.1.0-rc.5",
    "angular-messages": "1.5.7",
    "angular-resource": "1.5.7",
    "angular-sanitize": "1.5.7",
    "angular-ui-router": "1.0.0-beta.1",
    "jquery": "2.2.4",
    "mobile-detect": "1.3.2",
    "moment": "2.13.0"
  }

知道什么问题,我缺少什么吗?

【问题讨论】:

  • 你能确认你在 index.html 中有包含服务 SampleData 的 js 吗?
  • @gyc 我已经在编写 js 文件了,还没有移到 ts,仍然使用 angular 1.5 准备稍后使用 ts 进入第二阶段,我可以在控制器内部使用 myService,SampleData 没有独立的服务服务,它是一个在模块内部解析的变量,正如您在它们正在工作的第一个控制器和模块中看到的那样。

标签: angularjs angular-ui-router angularjs-components angularjs-1.5


【解决方案1】:
'use strict';
angular
    .module('app.sample')
    .controller('SampleController', SampleController);

/** @ngInject */
function SampleController(SampleData)
{
    var vm = this;
    vm.helloText = SampleData.data.helloText;
}

尝试在控制器中注入“SampleData”解析,而不是像上面那样给出,如下所示:

'use strict';
angular
    .module('app.sample')
    .controller('SampleController', ['SampleData', SampleController]);

/** @ngInject */
function SampleController(SampleData)
{
    var vm = this;
    vm.helloText = SampleData.data.helloText;
}

希望对你有用

【讨论】:

  • 控制器工作正常,我的组件有问题,我正在使用 .controller.component 删除
【解决方案2】:

终于解决了,我误解了组件是如何工作的。

首先我将SampleData 更改为sampleData,Pascal Case 但首字母小。

然后在module里面我把template改成了template: '&lt;sample sample-data="$resolve.sampleData"&gt;&lt;/sample&gt;'

resolve 到:

resolve: {
  sampleData: function (msApi) {
    return msApi.resolve('sample@get');
  }
}

对于component,我也更改了绑定:

bindings: {
  sampleData: '='
},

componentcontroller 中,我从签名中删除了SampleData,并像这样称呼它$ctrl.helloText = $ctrl.sampleData.data.helloText;

所以现在的最终代码是这样的: 对于模块

 (function () {
  'use strict';

  angular
    .module('app.sample', [])
    .config(config);

  /** @ngInject */
  function config($stateProvider) {
    // State
    $stateProvider
      .state('app.sample', {
        url: '/sample',
        views: {
          'content@app': {
            template: '<sample sample-data="$resolve.sampleData"></sample>'
          }
        },
        resolve: {
          sampleData: function (myService) {
            return myService.getSample(); // I return a promise here
          }
        }
      });
  }
})();

组件是这样的:

(function () {
  'use strict';

  angular
    .module('app.sample')
    .component('sample', {
      templateUrl: 'app/main/sample/sample.html',
      bindings: {
        sampleData: '='
      },
      controller: Sample
    });

  /** @ngInject */
  function Sample() {
    var $ctrl = this;
    $ctrl.helloText = $ctrl.sampleData.data.helloText;
  }
})();

终于成功了。

编辑: P.S.:在问答范围之外,如果你也使用没有状态的组件,你需要在控制器内部获取数据而不是解析,所以你可以在任何你想要的地方调用组件。

【讨论】:

  • 谢谢。我仍然无法在我的应用程序中使用它,所以我进行了一些搜索,发现这个 GitHub 问题有一个示例:github.com/angular-ui/ui-router/issues/2793
  • @jsparks 你用的是什么名字?无论如何避免使用data 字作为前缀或独立,
  • @Al-Mothafar 我想我可能遗漏了一些导致它无法工作的东西。第二个示例有助于重申您在回答中的内容。非常感谢。编辑:现在我想起来了,我在 GitHub 示例中看到的一个区别是它们的绑定是
  • @jsparks using '=' 是双向绑定,使用 '&lt;' 是一种方式,几乎相同,但是在两种方式中,您可以在对象中修改后取回值让说你希望在将组件内部处理给父级之后用数据填充属性(子级为父级赋值,反之亦然),但一种方法是从父级获取数据,就像只读属性一样。如果从父级更新,您需要使用this.$onChanges 处理此问题。
猜你喜欢
  • 2017-10-08
  • 2015-12-14
  • 2013-04-20
  • 2016-12-01
  • 2016-12-17
  • 2016-10-30
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
相关资源
最近更新 更多