使用嵌套路由angularjs时,"无法解决......从状态..."错误

Jus*_*ase 26 angularjs angular-ui-router

我是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>
Run Code Online (Sandbox Code Playgroud)

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'
    })
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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();
Run Code Online (Sandbox Code Playgroud)

这是视图(index.html)......

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

    <!-- Footer -->
    <footer></footer>
Run Code Online (Sandbox Code Playgroud)

我从控制台收到一条错误,指出,无法从状态'profile'解析"'edit_profile'",而edit.html应该是profile.html的子状态.我在angularjs中使用ui-routing.我希望能够单击profile.html中的一个按钮,该按钮将状态更改为edit_profile,并将显示在index.html的ui-view中.有关如何解决此问题的任何建议,还是有另一种简单的方法可以做到这一点?

Rad*_*ler 41

在这种情况下,角度通知我们:我根本找不到状态edit_profile.我在这里创建了工作示例

原因是,我们真的必须加载2个js文件:

  • profile.js
  • client_UI.js

必须加载它们,但它们都不能声明相同的模块:

var route = angular.module('route', ["ui.router"])
Run Code Online (Sandbox Code Playgroud)

其中一个必须是不同的或只是不声明但消耗模块:

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

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

// client_UI.js
var route = angular.module('route', ["ui.router"])
// profile.js
var route = angular.module('route')
Run Code Online (Sandbox Code Playgroud)

检查一切都在这里工作

  • ......是否有理由进行**投票?很高兴知道......能够扩展或改进答案...... (4认同)