我在角度向导样式webapp上有一些导航按钮。对于化妆品原因,他们需要从每个部分删除,并添加到根“index.html”:

<!-- Global navigation buttons for all partials -->
<div class="navbar navbar-fixed-top">
    <button class="btn btn-default" back-action>Back</button>
    <button class="btn btn-default" next-action>Next</button>
</div>

<div class="container ng-view ng-cloak">
     <!-- Partials rendered in here, managed by $routeProvider-->
</div>
.

我试图使用指令和范围变量隔离此逻辑以绑定点击赛事并为每个部分应用目标目的地:

.directive('nextAction', ['$location', function($location) {
    return {
        restrict: 'A',
        link: function(scope, elm) {
            elm.on('click', function () {
               var nextUrl = scope.nextUrl;
               $location.url(nextUrl);
            });
        }
    };
}])
.

然后在每个控制器中定义URL:

.controller('FirstStepCtrl', ['$scope', function ($scope) {
        $scope.backUrl = '/';
        $scope.nextUrl = '/first/second';
        ...
.

问题是生成icodetagcode是未定义,因为指令范围不会继承控制器范围。

随着它目前没有工作的事实,这种方法对我来说也有点脆弱,因为它依赖于嵌入在控制器代码中的导航逻辑。

如何创建基于当前“页面”动态重定向的更好的全局背/下按钮?

有帮助吗?

解决方案

使用状态管理器处理后面和下一个URL。减轻这个责任的控制者。然后将其注入处理后面和下一个按钮的指令。

.factory('stateMgr', ['$rootScope', function ($rootScope) {
    var stateMgr = {
        backUrl: '',
        nextUrl: ''
    };

    $rootScope.$on('$routeChangeSuccess', function (nextRoute, lastRoute) {
        // logic in here will look at nextRoute and then set back and next urls
        // based on new route   
        // e.g. stateMgr.backUrl = '/'; stateMgr.nextUrl = '/whatever';
    });

    return stateMgr;
}]);
.

然后

.controller('FirstStepCtrl', ['$scope', function ($scope) {
    // do not need to do anything with back/next urls in here
    ...
.

.directive('nextAction', ['$location', 'stateMgr', function($location, stateMgr) {
    return {
        restrict: 'A',
        link: function(scope, elm) {
            elm.on('click', function () {
                $location.url(stateMgr.nextUrl);
            });
        }
    };
}])
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top