简体   繁体   English

使用 angular.js ui-router 单击链接时出错

[英]Getting error while click on a link using angular.js ui-router

I am getting the following error while to click on a link using angular.js.使用 angular.js 单击链接时出现以下错误。

Error:错误:

Error: Could not resolve '.profile?p_i=3' from state 'dashboard.profile'
    at Object.t.transitionTo (angularuirouter.js:7)
    at Object.t.go (angularuirouter.js:7)
    at angularuirouter.js:7
    at angularjs.js:146
    at e (angularjs.js:43)
    at angularjs.js:45

I am using angular UI-router for nested views.我正在为嵌套视图使用 angular UI-router。 My code is below:我的代码如下:

profile.html:个人资料.html:

<div class="col-lg-12">
<div class="portlet portlet-blue" style="margin-bottom:12px;">
 <div class="portlet-body">
<div class="table-responsive dashboard-demo-table">
<table class="table table-bordered table-striped table-hover" id="dataTable" >
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-2 col-sm-2">
 <col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-1 col-sm-1">
</colgroup>
 <thead>
<tr>
<th>Sl. No</th>
<th>College Name</th>
<th>Address</th>
<th>Contact No</th>
<th>Edit</th>
</tr>
</thead>

<tbody id="detailsstockid">
<tr ng-repeat="profile in profileData">
<td>{{$index+1}}</td>
<td>{{profile.colg_name}}</td>
<td>{{profile.address}}</td>
<td>{{profile.cont_no}}</td>
<td>
<a ui-sref='.profile?p_i={{profile.profile_id}}'>
<input type='button' class='btn btn-xs btn-green' value='Edit'>  
</a>
</td>
</tr>   
</tbody>
</table>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>

This file is binding inside the dashboard.html page which is given below.该文件绑定在下面给出的dashboard.html 页面中。

dashboard.html:仪表板.html:

 <a ui-sref=".profile" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">College profile<span class="caret"></span></a>
<div class="row" style="padding-top:90px;"  ui-view>

        </div>

loginRoute.js:登录路由.js:

var Admin=angular.module('Channabasavashwara',['ui.router']);
Admin.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/');
    $stateProvider
     .state('/', {
            url: '/',
            templateUrl: 'dashboardview/login.html',
            controller: 'loginController'
        })
        .state('dashboard', {
            url: '/dashboard',
            templateUrl: 'dashboardview/dashboard.html',
            controller: 'dashboardController'
        })
        .state('dashboard.profile', {
        url: '/profile',
        templateUrl: 'dashboardview/profile.html',
        controller: 'profileController'
    })          
})

profileController.js: profileController.js:

var dashboard=angular.module('Channabasavashwara');
dashboard.controller('profileController',function($scope,$http,$location){
     var id=gup( "p_i" );
     $http({
        method: 'GET',
        url: "php/profile/readProfileData.php",
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).then(function successCallback(response){
        $scope.profileData=response.data;
    },function errorCallback(response) {

    });
     if(id != ''){
        $scope.buttonName="Update";
        var userdata={'userid':id};
        $http({
            method: 'POST',
            url: "php/profile/editProfileData.php",
            data: userdata,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function successCallback(response){
            console.log('edited data',response);
            $scope.colgname=response.data[0].colg_name;
            $scope.address=response.data[0].address;
            $scope.contno=response.data[0].cont_no;
        },function errorCallback(response) {
        });
    }else{
        $scope.buttonName="Submit";
    }
    $scope.addProfileData=function(){
        if($('#addProfileData')[0].defaultValue=='Submit'){
        if($scope.colgname==null){
            alert('course name field could not blank');
        }else if($scope.address==null){
            alert('short name field could not blank');
        }else if($scope.contno==null){
            alert('semester field could not blank');
        }else{
        var userdata={'colg_name':$scope.colgname,'address':$scope.address,'cont_no':$scope.contno};
        //console.log('userdata',userdata);
        $http({
            method: 'POST',
            url: "php/profile/addProfileData.php",
            data: userdata,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function successCallback(response){
            console.log('profile',response);
            alert(response.data['msg']);
            $scope.colgname=null;
            $scope.address=null;
            $scope.contno=null;
            $scope.profileData.unshift(response.data);
        },function errorCallback(response) {
            alert(response.data['msg']);
        })
        }
        }
        if($('#addProfileData')[0].defaultValue=='Update'){
            var updatedata={'colg_name':$scope.colgname,'address':$scope.address,'cont_no':$scope.contno,'profile_id':id};
            console.log('userupdate',userdata);
            $http({
            method: 'POST',
            url: "php/profile/updateProfileData.php",
            data: updateData,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function successCallback(response){
            alert(response.data);
            $location.path('profile');
        },function errorCallback(response) {
            alert(response.data);
        });
        }
    }
})
function gup( name ) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

Here when I am clicking on edit button in profile.html page this error is coming.在这里,当我单击 profile.html 页面中的编辑按钮时,此错误即将发生。 I need to move to same profile page again after click.单击后我需要再次移动到相同的个人资料页面。

There is a working example一个工作示例

Easily said:轻松地说:

relative path could be used only in a proper context...相对路径只能在适当的上下文中使用...

So, inside of views of the state 'dashboard' :因此,在状态'dashboard'的视图内部:

.state('dashboard', {
    url: '/dashboard',
    templateUrl: 'dashboardview/dashboard.html',

Namily in this case inside of the view 'dashboardview/dashboard.html' - we can use realtive path:在这种情况下,在视图“dashboardview/dashboard.html”内,我们可以使用真实路径:

<a ui-sref='.profile?p_i={{profile.profile_id}}'>

because it will be provided with proper starting part:因为它将提供适当的起始部分:

current state ( 'dashboard' ) + relative path '.profile' === 'dashboard.profile'当前状态 ( 'dashboard' ) + 相对路径'.profile' === 'dashboard.profile'

In any other state , we will get different result - of the relative path.任何其他状态下,我们将得到不同的结果- 相对路径。 Mostly targeting NON EXISTING states...针对大多不存在的状态...

EXTEND: to make it running (and later play with relative state names) I would suggest to use absolute state name: EXTEND:为了使其运行(然后使用相对状态名称),我建议使用绝对状态名称:

// instead of this
<a ui-sref='.profile?p_i={{profile.profile_id}}'>
// I'd use this
<a ui-sref='dashboard.profile'>

And to be able to consume the parameter, we should extend state defintion with url param:为了能够使用参数,我们应该使用 url 参数扩展状态定义:

.state('dashboard.profile', {
        url: '/profile?profileId',
        // or
        url: '/profile/:profileId',
        templateUrl: 'dashboardview/profile.html',
        controller: 'profileController'

And then we can pass param like this然后我们可以像这样传递参数

<a ui-sref='dashboard.profile({profileId:profile.profile_id})'>

There is a working example一个工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM