繁体   English   中英

角度指令的路由问题

[英]Routing issue with angular directives

我正在尝试设置我的第一个有角度的项目,但是在掌握布线方面遇到困难。

在页面加载时,我看到由preferencesDirective设置的初始模板非常好。

当我单击“更改模板”按钮时,我希望它更改为另一个模板,但是什么也没有发生。 如果我将$ routeProvider中的模板URL设置为无效,那么我会在调试器中看到404错误,该错误告诉我某些东西必须在工作,但是当模板URL有效时什么也没发生。如何正确更改它?

谢谢。

<div id="PreferencesApp" class="" ng-app="clientPreferencesModule">    
    <preferences-directive factory-settings="clientPreferences"></preferences-directive>
    <a href="#Details">Change Template</a>
</div>


<script>

    var app = angular.module("clientPreferencesModule", ["ngResource", "ngRoute"]);
    //var app = angular.module("clientPreferencesModule", ["ngRoute"]);


    app.config(function ($routeProvider) {        
        $routeProvider.when("/", { controller: "clientPreferencesController", templateUrl: '/AngularTemplates/ClientPreferences/PreferencesTemplate.html' });
        $routeProvider.when("/Preferences/:id", { controller: "clientPreferencesController", templateUrl: '/AngularTemplates/ClientPreferences/PreferencesTemplate.html' });
        $routeProvider.when("/Preferences", { controller: "clientPreferencesController", templateUrl: '/AngularTemplates/ClientPreferences/PreferencesTemplate.html' });
        $routeProvider.when("/Details", { controller: "clientPreferencesController", templateUrl: '/AngularTemplates/ClientPreferences/DetailsTemplate.html' });                
    });



    app.controller('clientPreferencesController', clientPreferencesController);

    clientPreferencesController.$inject = ["$scope", "$resource", "$rootScope", "$http", "$route", "$location"];

    function clientPreferencesController($scope, $resource, $rootScope, $http, $route, $location) {        
        this.model = @Html.Raw(JsonConvert.SerializeObject(Model));        
        $scope.location = $location.path();        
    }

    app.directive('preferencesDirective', preferencesDirective);

    function preferencesDirective() {

        return {
            restrict: 'EA',
            scope:
            {
                factorySettings: '='
            },
            controller: 'clientPreferencesController',
            controllerAs: 'pc',
            bindToController: true,
            templateUrl: '/AngularTemplates/ClientPreferences/PreferencesTemplate.html'
        }
    }

</script>

为了使路由正常工作,您必须创建不同的视图及其关联的控制器,然后在该视图内使用指令。 另外,您还需要在index.html中使用ng-view指令,在该指令中将加载所有路由视图。 而且preferencesDirective应该只包含可重用的独特功能以及完整的应用程序视图,以便您可以在具有不同数据集以及不同视图组件的情况下拥有不同的视图。 因此,您的模板可以是:

<div id="PreferencesApp" class="" ng-app="clientPreferencesModule">    
    <a href="#Details">Change Template</a>
    <div ng-view></div>
</div>

现在,对于不同的路由,您可以具有每个不同的控制器,或者如果您想在一个控制器中处理它,则只能有一个控制器,但与指令的控制器不同,因此可以是,

app.config(function ($routeProvider) {        
    $routeProvider.when("/", { controller: "viewController", templateUrl: '/AngularTemplates/ClientPreferences/PreferencesTemplate.html' });
    $routeProvider.when("/Preferences/:id", { controller: "viewController", templateUrl: '/AngularTemplates/ClientPreferences/PreferencesTemplate.html' });
    $routeProvider.when("/Preferences", { controller: "viewController", templateUrl: '/AngularTemplates/ClientPreferences/PreferencesTemplate.html' });
    $routeProvider.when("/Details", { controller: "viewController", templateUrl: '/AngularTemplates/ClientPreferences/DetailsTemplate.html' });                
});

在所有这些模板中都有preferencesDirective。 (这现在可能会更改指令的模板,但是您可以在视图的模板中更改每个视图的dom并保持指令的模板不变)。现在在viewController中,通过使用$ routeParams您可以检查当前路由并将不同的数据发送到preferencesDirective的控制器。

现在,如果您必须要有条件地更改指令模板,则可以在指令模板中使用ng-include。

function preferencesDirective() {

    return {
        restrict: 'EA',
        scope:
        {
            factorySettings: '=',
            templateSrc: '='
        },
        controller: 'clientPreferencesController',
        controllerAs: 'pc',
        bindToController: true,
        templateUrl: '<ng-include src="pc.template()"></ng-include>'
    }
}

function clientPreferencesController($scope, $resource, $rootScope, $http, $route, $location) {        
    this.model = @Html.Raw(JsonConvert.SerializeObject(Model));        
    $scope.location = $location.path();     
    $scope.template = function(){
        if($scope.templateSrc) {
            return '/AngularTemplates/ClientPreferences/'+ $scope.templateSrc + '.html';
        }
    }
}

这里基于当前路由从viewController共享那个templateSrc。

暂无
暂无

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

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