我需要做什么才能将值作为属性从角度模板传递到指令

What do I need to do to pass value as attribute from angular template to directive?

本文关键字:指令 属性 什么      更新时间:2023-09-26

我有一个看起来像这样的指令:

<g-map-locations center={{myLocation}} zoom="4" id="map" class="map"></g-map-locations>
缩放

值以角度用于设置谷歌地图的缩放比例:

attrs.zoom = zoom
setMapOptions: function(center, zoom){
    var mapOptions = {
    zoom: zoom,
    center: center
    }
    return mapOptions;
},

谷歌地图抱怨setZoom: is not a number,虽然它有效我做

zoom = 4

我可以告诉 angular 将值作为数字传递或以某种方式在指令中转换它吗?

HTML 属性作为字符串传入,除了当它们作为作用域对象传入并解析时。在我看来,最简单的解决方案是将数字解析为整数:

setMapOptions: function(center, zoom){
    var mapOptions = {
        zoom: parseInt(zoom, 10),
        center: center
    }
    return mapOptions;
}