gpt4 book ai didi

javascript - Angular Js -> UI - 路由 -> 解决问题

转载 作者:行者123 更新时间:2023-11-30 16:34:18 27 4
gpt4 key购买 nike

我正在使用 ajs(1.4.7) 和 angular-ui-router(0.2.15) 开发一个简单的 AJS 应用程序。

经历了this文章并选择了路由解析技术。

这是我遇到的错误

错误:[$injector:unpr] 未知提供者:actorGridDataProvider <- actorGridData <- mainController http://errors.angularjs.org/1.4.7/ $injector/unpr?p0=actorGridDataProvider%20%3C-%20actorGridData%20%3C-%20mainController 在 REGEX_STRING_REGEXP(angular.js:68) 在 angular.js:4289 在 Object.getService [as get] (angular.js:4437) 在 angular.js:4294 在 getService (angular.js:4437) 在 Object.invoke (angular.js:4469) 在 ident.$get.extend.instance (angular.js:9136) 在 nodeLinkFn (angular.js:8248) 在 compositeLinkFn (angular.js:7680) 在 publicLinkFn (angular.js:7555)

我猜测在调用 mainController 之前未定义“actorGridData”。我正在尝试在加载应用程序时在主页上填充一个网格。因此,解决技术方法。

路由器代码:

    'use strict';

imdbapp.config(function($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/homeGridPage');

$stateProvider

.state('homeGridPage', {
url : '/homeGridPage',
templateUrl : 'uiapp/imdbapp/models/commonPages/homeGridPage.html',
controller: 'mainController',
resolve :{
'actorGridData' : function($stateParams, imdbHttpService){
return imdbHttpService.loadAllActors();
}
}
});
});

这是 Controller 代码:

/**
* Home Page Controller for IMDB App.
*/
'use strict';
imdbapp.controller('mainController', function($scope, $http, $rootScope, $location, imdbHttpService, actorGridData) {

$scope.init = function() {
/**
* un-commenting this line calls the service after controller being
* activated (Controller Activate). This is working as expected and grid
* loads after application is instantiated.
*/
// $scope.loadAllActors({});
$scope.actorGridData = actorGridData;
};

$scope.loadAllActors = function(inputArgs, outputArgs) {
imdbHttpService.loadAllActors().then(function(actorListHTTPResponseData) {
$scope.actorGridData = [];
if (actorListHTTPResponseData != null && actorListHTTPResponseData.actorList.length > 0) {
$scope.actorGridData = actorListHTTPResponseData.actorList;
}
}, function(errorMessage) {
$scope.error = errorMessage;
});
};

$scope.imdbActorGridOptions = {
data : 'actorGridData',
showGridFooter : true,
resizable : true,
sortable : false,
enableFiltering : false,
columnDefs : [ {
name : 'actor_id',
displayName : 'Actor Id',
enableFiltering : true,
width : 100,
pinnedLeft : true,
sortable : false
}, {
name : 'first_name',
displayName : 'First Name',
width : 155
}, {
name : 'last_name',
displayName : 'Last Name',
width : 160
} ]
};

});

index.html

<!DOCTYPE html>
<!-- define angular app -->
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- SCROLLS -->
<link rel="stylesheet" href="http://ui-grid.info/release/ui-grid.css"
type="text/css">
<link
href="//netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"
rel="stylesheet">
<!-- Custom styles for this template -->
<link href="uiapp/imdbapp/styles/styles.css" rel="stylesheet">

<title>IMDB AJS app</title>
</head>

<!-- define angular controller ng-controller="mainController"-->
<body ng-app="imdbapp" ng-controller="mainController">
<div id="wrap">
<nav class="navbar navbar-default" role="navigation">
<!-- Header and Navigation section -->
<div ng-include="'uiapp/imdbapp/models/header/application-navigation.html'"></div>
</nav>

<!-- angular templating; this is where content will be injected -->
<div ui-view></div>
</div>
<!-- Footer section -->
<footer class="footer">
<div ng-include="'uiapp/imdbapp/models/footer/footer.html'"></div>
</footer>

<!-- JavaScript libraries -->
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>

<!-- AJS libraries -->
<!-- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script> -->
<script src="uiapp/imdbapp/components/angular/1.4.7/angular.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-date/0.0.8/date.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-messages.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular-resource.min.js"></script>
<!-- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular-route.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js"></script>
<script src="http://ui-grid.info/release/ui-grid.js"></script>

<!-- Main app file -->
<script src="uiapp/imdbapp/scripts/app.js"></script>

<!-- imdbapp controllers -->
<script src ="uiapp/imdbapp/scripts/routes/imdb-route.js"></script>

<!-- imdbapp services -->
<script src =uiapp/imdbapp/scripts/services/HTTPServiceWrapper.js></script>
<script src ="uiapp/imdbapp/scripts/services/ActorServices.js"></script>

<!-- imdbapp routes -->
<script src ="uiapp/imdbapp/scripts/controllers/UserController.js"></script>


<!-- imdbapp directives -->


</body>
</html>

我已经为 HTTP 调用编写了一个包装器。如果此信息不充分,我可以发布代码。

如果我忽略了,请帮助我。提前致谢。当我选择 ngRoute 而不是 ui-route 时,也会发生同样的事情。

最佳答案

当你使用 ui-router 时,你不应该使用 ng-controller任何地方。您的 Controller 会自动实例化为 ui-view当它们的适当状态被激活时。

更改 <body ng-app="imdbapp" ng-controller="mainController"><body ng-app="imdbapp"> .

关于javascript - Angular Js -> UI - 路由 -> 解决问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32897910/

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