使用嵌套路由 angularjs 时出现"could not resolve ... from state ..."错误



我是angularjs的新手,我想在我的应用程序中使用嵌套的ui-routes。一些代码片段…

profile.html

<div class="row">
<div class="dl-horizontal" id="information">
    <div class="col-md-8 col-md-offset-2">
    <!-- edit button -->
    <div style="margin-bottom: 15px">   
        <div class="button" style="margin-top:5px" style="float:left">
            <button type="button" class="btn btn-primary" ui-sref="edit_profile" ng-click="populate()"><span class="glyphicon glyphicon-pencil"></span> Edit Profile</button>
        </div>
    </div>

client_UI.js

//object for routing
var route = angular.module('route', ["ui.router"])
// configure the routing        
route.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
    // send to profile page
    $urlRouterProvider.otherwise("/profile");
    $stateProvider
    // route for personal info
    .state('profile', {
        url: "/profile", 
        templateUrl : "profile_area/profile.html" , 
        controller : 'profileController'
    })

edit.html

<script src="Scripts/angular-ui-bootstrap.js"></script>

    <!-- title -->
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h2 class="modal-title"> Editing Profile</h2>
    </div>
    <div class="modal-body">
        <form role="form" id="myForm">
            <!-- Name -->
        <div class="panel panel-default">
        <div class="panel-body">
            <div class="form-group">
                <label for="name">Name &nbsp;</label>
                <input placeholder="{{infos.Name}}" id="name" type="text" ng-model="name">
            </div>

profile.js

//object for routing
var route = angular.module('route', ["ui.router"])
// configure the routing        
route.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
$stateProvider
//route for edit page
.state('edit_profile', {
    url: "/edit_profile",
    templateURL : "edit/edit_profile.html" ,
    controller : 'editController'
})
});
route.controller('editController' ["$scope", "$resource", function($scope, $resource) {
// resource objects
var profile = $resource($scope.apicall + "/api/userprofile/", {Userid:$scope.userid}, {'post':{method: 'POST'}});
var edit = new profile();

,这是视图(index.html)…

       <!-- route veiw -->
    <div class="container" id="route" style="width:90%">
            <div ui-view></div>
    </div>
    <!-- Footer -->
    <footer></footer>

我从控制台得到一个错误,说,无法解析"'edit_profile'从状态'profile'"和edit.html应该是profile.html的子状态。我在angularjs中使用ui-routing。我希望能够在profile.html中单击一个按钮,这将改变状态为edit_profile,并将在index.html的ui视图中显示。有什么建议如何解决这个问题,或者有另一种简单的方法来做到这一点?

在这种情况下,angular告诉我们:我根本找不到状态edit_profile 。我在这里创建了一个工作示例

原因实际上是,我们确实需要加载2个js文件:
  • profile.js
  • client_UI.js

它们必须同时加载,但是它们不能同时声明同一个模块:

var route = angular.module('route', ["ui.router"])

它们中的一个必须是不同的,或者只是不声明而使用模块:

// client_UI.js
var route = angular.module('client_ui', ["ui.router"])
// profile.js
var route = angular.module('route', ["ui.router", "client_ui"])

或者两者都可以在同一个模块中:

// client_UI.js
var route = angular.module('route', ["ui.router"])
// profile.js
var route = angular.module('route')

检查所有工作

相关内容

最新更新