如何使用angular重定向用户

时间:2014-07-16 09:36:16

标签: javascript angularjs

我试图通过这种方式使用角度进入我的网络应用程序时点击提交按钮来转发用户:

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);
        }
    });

})();

但是,这不会起作用。

请问如何解决这个问题?

提前致谢!

2 个答案:

答案 0 :(得分:1)

使用location service

  

$ location service为只读部分提供getter方法   URL(absUrl,协议,主机,端口)和getter / setter方法   网址,路径,搜索,哈希:

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

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

答案 1 :(得分:0)

如果我想考虑角度重定向,我会首先听取$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');
        }
});