得到“无法解决......从状态......"使用嵌套路由angularjs时出错嵌套、路由、状态、angularjs

2023-09-06 08:31:50 作者:相关好听的昵称推荐>>

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

I'm new to angularjs and I want to use nested ui-routes for my application. Some code snippets ...

profile.html

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

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) ...

and this is the view (index.html) ...

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

    <!-- Footer -->
    <footer></footer>

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

I'm getting an error from the console that says, Could not resolve "'edit_profile' from state 'profile'" and edit.html is supposed to be the child state of profile.html. I'm using ui-routing in angularjs. I want to be able to click a button in the profile.html that will change the state to edit_profile and will be displayed in the ui-view in index.html. Any suggestions on how to fix this or is there another easy way to do this?

推荐答案

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

In this case, angular is informing us: I simply cannot find the state edit_profile. I created working example here

原因其实是我们真的要加载2个js文件:

The reason is in fact, that we really have to load 2 js files:

profile.jsclient_UI.js

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

both of them must be loaded but both of them cannot declare the same module:

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

其中一个必须不同,或者只是不声明但使用模块:

one of them must be different or just not declare but consume the module:

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

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

or both could be in same module:

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

检查所有工作这里

 
精彩推荐
图片推荐