Angular 部分 header 视图

Angular partial header view

在尝试学习 AngularJS 结构的最佳实践时,我在几个地方读到部分视图应该作为指令实现,这就是我为我的应用所做的 header.

现在,对于我的特定 header,我有一个后退按钮,它应该只在不在主页上时可见。在终于让它工作之后,我不确定我是否对我的解决方案感到满意。例如,它不使用 var vm = this; 的 controllerAs 方法,如果可以避免的话,我宁愿根本不让它有专用的 scope/controller。

这是我的 headerView 的指令代码:

(function () {

    'use strict';

    function appHeader () {
        return {
            restrict: 'E',
            templateUrl: 'shared/header/headerView.html',
            controller: function ($scope, $route, $location) {
                $scope.homeUrl = 'start/' + $route.current.params.id;
                $scope.isHomeUrl = function () {
                    if($location.path() == $scope.homeUrl) {
                        return true;
                    }
                    return false;
                };
                $scope.backClick = function() {
                    window.history.back();
                    return false;
                };
            }
        }
    };

    angular.module('myApp').directive('appHeader', appHeader);

})();

你觉得我的 header 有后退按钮,你会用另一种方式来做吗?建议?

Angular UI Router 比指令更能满足您的需求。

看看这个question,写得很好,还有一个可以工作的插件!

至于 controllerAs 方法,看看这个 Whosebug question