簡體   English   中英

如何使用角度重定向用戶

[英]how to redirect user using angular

我正在嘗試使用以下方式將用戶單擊使用angular提交到我的網絡應用程序中的“提交”按鈕:

HTML代碼

    <tr ng-controller="fCtrl as fiche">
        <td>
            <span ng-hide="fiche.canBeEdited()"></span>
            <span ng-show="fiche.canBeEdited()">
                <input type="checkbox" name="qualif0" ng-model="qualif0" >
            </span>
        </td>
        <td>
            <span ng-hide="fiche.canBeEdited()"></span>
            <span ng-show="fiche.canBeEdited()">
                <input type="checkbox" name="qualif1" ng-model="qualif1" >
            </span>
        </td>
        <td>
            <span ng-hide="fiche.canBeEdited()"></span>
            <span ng-show="fiche.canBeEdited()">
                <input type="checkbox" name="commentaire" ng-model="commentaire" >
            </span>
        </td>
        <td>
            <a href="#" ng-click="fiche.editLine()" title="Modifier">Modifier</a>
            <button type="submit" class="btn btn-primary" ng-show="fiche.canBeEdited()" ng-click="fiche.submitEdit(qualif0, qualif1, commentaire)">Modifier</button>
        </td>
    </tr>

app.js

(function(){
    var app = angular.module('m', [ ]);

    app.controller('fCtrl', function(){
        this.edit = 0;

        this.editLine = function(){
            this.edit = 1;
        };

        this.canBeEdited = function(){
            return this.edit === 1;
        }

        this.submitEdit = function(qualif0, qualif1, commentaire){
            this.edit = 0;
            window.location.replace('./fiche/traiter?qualif0='+qualif0+'&qualif1='+qualif1+'&commentaire='+commentaire);
        }
    });

})();

但是,這行不通。

請如何解決此問題?

提前致謝!

使用位置服務

$ location服務為URL的只讀部分(absUrl,協議,主機,端口)提供getter方法,並為url,path,search,hash提供getter / setter方法:

// get the current path
$location.path();

// change the path
$location.path('/newValue')

如果我想考慮角度重定向,我會先聽$routeChangeStart事件(我認為也有$locationChangeStart ),然后使用$location.path()從那里重定向。 這樣,我將避免在重定向之前對路由進行有角度的加載。

這來自我的一些舊代碼:

以下代碼需要處於較高的范圍(例如,包括所有頁面組件的容器。容器)。之所以存在,是因為它需要在所有視圖上都處於活動狀態。

// listening to the routeChangeStart event. It is triggered every time the router navigates between views 
$rootScope.$on('$routeChangeStart', function(event, newLocation, oldLocation)
        // check if the route is allowed
        if( newLocation !== '/private'  ){
            // if not we stop the navigation and redirect to something else;
            event.preventDefault();
            $location.path('/notAuthorized');
        }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM