简体   繁体   English

提供基于范围变量的Angular指令模板

[英]Provide a template for Angular directive based on scope variable

I am newbie for Angular, and I need to implement interactive map with selectable parts of svg file. 我是Angular的新手,我需要使用svg文件的可选部分来实现交互式地图。

There is my controller, and directives for whole svg map and partial regions. 有我的控制器,以及整个svg映射和部分区域的指令。

ColorPicker controller ColorPicker控制器

app.controller("ColorPicker", function($scope, $timeout, appConfig) {
$scope.colors = [];
$scope.currentColorSet = 0;
$scope.svgTemplateUrl = appConfig.arSvg[$scope.$parent.roomType.id][$scope.$parent.roomStyle.id]+'over.svg'

$scope.alertColor = function(color) {
    console.log(color);
}


});

Map directive 地图指令

app.directive('svgMap', ['$compile', function ($compile) {
return {
    restrict: 'A',
    templateUrl: 'models/livingrooms/Livingroom_01/over.svg',
    link: function (scope, element, attrs) {

        var svg = $(element).find('svg');
        svg.removeAttr('xmlns:a');
        svg.attr('width', '869px');
        svg.attr('height', '489px');
        element.append(svg);

        var regions = element[0].querySelectorAll('path');

        angular.forEach(regions, function (path, key) {
            var regionElement = angular.element(path);
            regionElement.attr("region", "");
            $compile(regionElement)(scope);
        });
    },
}
}]);

Region directive 地区指令

app.directive('region', ['$compile', function ($compile) {
return {
    restrict: 'A',
    scope: true,
    link: function (scope, element, attrs) {
        scope.elementId = element.attr("id");
        scope.stroke = '';
        scope.regionClick = function () {
            console.log(scope.elementId);
        };
        scope.setStroke = function () {
            scope.stroke = '#e5514e';
        };
        scope.removeStroke = function () {
            scope.stroke = '';
        };
        element.attr("ng-click", "regionClick()");
        element.attr("ng-attr-stroke", "{{stroke}}");
        element.attr("ng-mouseenter", "setStroke()");
        element.attr("ng-mouseleave", "removeStroke()");
        element.removeAttr("region");
        $compile(element)(scope);
    }
}
}]);

What I need is to dynamically load SVG file as template and compile it and all it regions based on scope variable $scope.svgTemplateUrl 我需要动态加载SVG文件作为模板,并根据作用域变量$ scope.svgTemplateUrl对其进行编译以及所有区域

All working fine now if templateUrl is a constant string, how to make it dynamic? 如果templateUrl是一个常量字符串,那么现在一切正常,如何使其动态化?

Ok if you want to load templateUrl dynamically then it can be done like this by passing a callback to it. 好的,如果您想动态加载templateUrl则可以通过向其传递回调来完成。

templateUrl: function(element,attrs) {
             return attrs.templateUrl;
           }

Now while using the directive- 现在,在使用指令时-

<svgMap template-url="models/livingrooms/Livingroom_01/over.svg"><svgMap>

alternative way is to define a scope directive like 另一种方法是定义一个范围指令,例如

app.directive('svgMap', ['$compile', function ($compile) {
return {
    restrict: 'A',
    scope: {
        templatecontenturl: '='
    },
    templateUrl: templatecontenturl,
    link: function (scope, element, attrs) {

    },
}
}]);

Use it like 像这样使用

<svgMap templatecontenturl="svgTemplateUrl"><svgMap>

See the http://weblogs.asp.net/dwahlin/creating-custom-angularjs-directives-part-2-isolate-scope on scope variable passing between directives. 有关在指令之间传递的范围变量的信息,请参见http://weblogs.asp.net/dwahlin/creating-custom-angularjs-directives-part-2-isolate-scope

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

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