我正試圖在兩個指令之間進行通信。我嘗試了服務方式,但沒有奏效。也許我做錯了什麼。在兩個指令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,也不能創建新的控制器。我將在一個相當大的企業應用程序中使用這裏的邏輯,只是爲了讓邏輯正確,我在本地主機上做了一個非常簡單的版本。