gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:15:49 25 4
gpt4 key购买 nike

我有两个指令,第一个指令有一个名为“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com