[英]Pass JSON object from Angular service to directive
我正在尝试将一个JSON对象从Angular服务传递给一个指令。 其实我只是想$compile
一个指令在这去 ,并通过对象的指令。
它应该看起来像这样:
var template = '<gmap-info-window layer="layer" marker="marker"></gmap-info-window>',
content = $compile(template)(searchScope);
该指令如下所示:
.directive('gmapInfoWindow', [function() {
scope: {
marker: '=',
layer: '='
},
link: function(scope, element, attrs) {
// access objects in attrs
}
}]);
这不起作用。 所有我进入的attrs.marker和attrs.layer都是简单的字符串。
现在我已经试过和accomlished使用transcludeFn
的功能$compile
功能。 它有效,但我觉得这不是我正在努力完成的正确方法。
var template = '<gmap-info-window></gmap-info-window>',
content = $compile(template)(searchScope, null, {
parentBoundTranscludeFn: function() {
return {
marker: _marker,
layer: _layer
};
}
});
该指令如下所示:
.directive('gmapInfoWindow', [function() {
scope: {},
link: function(scope, element, attrs, controller, transcludeFn) {
var objects = transcludeFn();
// The marker and layer are in objects now!
}
}]);
我无法想象没有其他方法可以做我想做的事情。 这看起来有点脏。 感谢您的见解!
所有我进入的attrs.marker和attrs.layer都是简单的字符串。
您需要了解该属性始终是一个字符串。 你不可能有一个对象。 Angular的作用是根据指令的范围配置在适当的上下文(编译范围)中评估这些属性(字符串)的值。 然后,该评估的结果可在链接函数的scope
对象中获得。 这是您需要使用的:
link: function(scope, element, attrs) {
console.log(scope.marker, scope.layer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.