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

Getting an attribute of a directive from another directive?

我有两个指令,第一个指令有一个名为layer-view的属性,由指令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();
            }
        };
    }
})();

我想访问指令 imapGrid

layer-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