javascript - 从另一个指令获取指令的属性?

标签 javascript angularjs angularjs-directive directive

我有两个指令,第一个指令有一个名为“layer-view”的属性,该属性由指令中的 Controller 填充 imapGrid 希望捕获此值属性

html

<ng-map mapid="mapid" layers-view="geo"></ng-map>
<imap-grid></imap-grid>

ng.map.directive.js

(function() {

    'use strict';

    angular
        .module('app')
        .directive('ngMap', ngMap);

    function ngMap($q, $parse) {
        return {
            restrict: "E",
            replace: true,
            scope: {
                layersView: '='
            },
            transclude: true,
            controller: function($scope) {
                this.getScope = function() {
                    return $scope;
                };
            },

            link: function(scope, element, attrs, ctrl) {

            }
        };
    }

})();

imap.grid.directive.js

(function() {

    'use strict';

    angular
        .module('app')
        .directive('imapGrid', imapGrid);

    function imapGrid($q, $parse) {
        return {
            restrict: "E",
            scope: true,
            replace: false,
            templateUrl: 'src/templates/imapGrid/grid.html',
            require: '?^ngMap',
            link: function(scope, element, attrs, ctrl) {
                var scope = ctrl.getScope();
            }
        };
    }
})();

我想访问指令imapGridlayer-view属性的内容

最佳答案

我现在看到的问题是您指的是 require: '?^ngMap'在你的代码中..但是你的html <ng-map mapid="mapid" layers-view="geo"></ng-map> <imap-grid></imap-grid> 清楚地表明ng-map不是 imap-grid 的父指令

所以使用require: '?ngMap'获取 ngMap,因为它是 imapGrid 的同级指令

来源:https://docs.angularjs.org/api/ng/service/$compile

关于javascript - 从另一个指令获取指令的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976073/

相关文章:

angularjs - 观看 iScroll 的 Angular 指令

javascript - 无法查看 Angular 指令属性

javascript - 当用户在屏幕上拖动/平移时 Canvas 清晰

javascript - 在javascript中访问全局变量时出现问题

javascript - 如何在 Angular 中循环和生成文本

javascript - 如何在单独的 div 中显示 ng-options 值中的值

javascript - angularjs嵌套指令范围隔离隐藏父指令对象

javascript - 为 TypeScript 中的 JavaScript 函数导出声明文件

javascript - 拖动鼠标右键时如何平移 div 的内容?

angularjs - 如何有条件地隐藏 Angular 指令的元素