我正试图在两个指令之间进行通信。我尝试了服务方式,但没有奏效。也许我做错了什么。在两个指令Angularjs之间进行通信?
<list-data testing="trial" template-url="grid.html" link-passed="data.json"></list-data>
我的指导和服务:
app.directive('listData', function($http, serVice){
return {
restrict: 'E',
scope: {
testing: '@',
linkPassed: '@'
},
templateUrl: function(elem,attrs) {
return attrs.templateUrl || 'some/path/default.html'
},
link: function($scope){
var url = 'js/' + $scope.linkPassed;
$http.get(url).then(success, error);
function success(data){
$scope.iconUrl = data.data;
}
function error(response){
console.log(response)
}
$scope.tryingToClick = function(icon){
serVice=icon.name;
console.log(serVice)
}
}
};
});
app.directive('render', function(serVice){
return {
restrict: 'E',
template: '{{rendering}}',
link: function($scope){
$scope.rendering = serVice.name;
console.log(serVice)
}
};
});
app.factory('serVice', function(){
return{items:{}};
})
grid.html就是在那里我试图显示网格中的数据一个简单的网格布局。
<div class="col-sm-6 grid" ng-repeat="icon in iconUrl">
<p ng-click="tryingToClick(icon)">{{icon.iconUrl}}</p>
</div>
当我单击函数tryingToClick并将图标传递给render指令时,我需要传递数据。我不能在这里使用$ rootcope,也不能创建新的控制器。我将在一个相当大的企业应用程序中使用这里的逻辑,只是为了让逻辑正确,我在本地主机上做了一个非常简单的版本。