gpt4 book ai didi

javascript - Angular ui-router 无法加载,更不用说解决工厂依赖关系了

转载 作者:行者123 更新时间:2023-11-28 19:40:49 25 4
gpt4 key购买 nike

只要我不使用resolve,UI 路由器就可以正常工作。我无法理解这些技巧。我将感谢您帮助理解这个问题。以下是代码。

var myApp = angular.module('myApp', ['ui.router']);

UI 路由器运行中[不工作] - 控制台不打印任何内容。

myApp.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
$stateProvider.state('state1', {
url: '/',
resolve: {
param1: function (RandomFactory) {
console.log('test');
return RandomFactory();
}
},
controller: 'RandomController',
});
$urlRouterProvider.otherwise("/");
}]);

UI 路由器[工作] - 控制台打印。

myApp.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
$stateProvider.state('state1', {
url: '/',
resolve: {
param1: function () {
console.log('test');
return "hello";
}
},
controller: 'RandomController',
});
$urlRouterProvider.otherwise("/");
}]);

工厂

myApp.factory('RandomFactory', ['', function () {
return "Hello there";
}])

Controller

myApp.controller('RandomController', ['$scope', function ($scope, param1) {
$scope.param = param1;
}]);

我已阅读以下帖子。

  1. angular-ui router -- inherited resolved dependencies for NAMED dependencies
  2. Angular ui-router fails to resolve named dependencies
  3. Angular-ui-router: managing dependencies with resolve
  4. Promise dependency resolution order in angular ui-router

也许我遗漏了一些东西,但我已经在这上面花了 5 个多小时了。

这是一个 fiddle - http://jsfiddle.net/kshitij/NPN7T/8/

<小时/>

解决方案

Empty strings in the factory definition was the issue. Though the framework should define an error for this case as it'll help new comers.

最佳答案

请参阅此处:http://plnkr.co/edit/cyZPh5h1lIYykqrh8Igh?p=preview

var myApp = angular.module('myApp', ['ui.router']);

myApp.factory('RandomFactory', function () {
return "Hello there";
});

myApp.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
$stateProvider.state('state1', {
url: '/state1',
template:"<p>{{param1}}</p>",
controller: function($scope,param1) {

$scope.param1 = param1;

},
resolve: {
param1: function (RandomFactory) {
return RandomFactory;
}
}
});
$urlRouterProvider.otherwise("/state1");
}]);

工厂定义中几乎没有错误,它应该是这样的(删除空字符串)

myApp.factory('RandomFactory', [function () {
return "Hello there";
}])

并且您应该在解析它时返回 RandomFactory 而不是执行它

resolve: {
param1: function (RandomFactory) {
return RandomFactory;
}
}

关于javascript - Angular ui-router 无法加载,更不用说解决工厂依赖关系了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25069660/

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