我是Angular.js的新手,我需要在我的应用程序之间进行指令之间的一些通信,我阅读了一些关于链接和需求的文档,但是无法准确理解它是如何工作的.
我有一个简单的例子:活小提琴:http://jsfiddle.net/yw235n98/5/
HTML:
<body ng-app="myApp">
First Directive :
<first-dir >
<h3>{{firstCtrl.data}}</h3>
<button ng-click="firstCtrl.set('NEW VALUE')">Change Value</button>
</first-dir>
Second Directive :
<second-dir>
<h3>{{secondCtrl.data}}</h3>
</second-dir>
Run Code Online (Sandbox Code Playgroud)
Javascript:
(function(){
var app = angular.module('myApp', []);
app.directive("firstDir", function(){
return {
restrict : 'E',
controller : function(){
this.data = 'init value';
this.set = function(value){
this.data = value;
// communication with second Directive ???
}
},
controllerAs : 'firstCtrl'
};
});
app.directive("secondDir", function(){
return {
restrict : 'E',
controller : function(){
this.data …Run Code Online (Sandbox Code Playgroud) 我有一个JSON结构如下
var myJson = {
"Number of Devices":2,
"Block Devices":{
"bdev0":{
"Backend_Device_Path":"/dev/ram1",
"Capacity":"16777216",
"Bytes_Written":9848,
"timestamp":"4365093970",
"IO_Operations":87204,
"Guest_Device_Name":"vdb",
"Bytes_Read":107619,
"Guest_IP_Address":"192.168.26.88"
},
"bdev1":{
"Backend_Device_Path":"/dev/ram2",
"Capacity":"16777216",
"Bytes_Written":10062,
"timestamp":"9365093970",
"IO_Operations":93789,
"Guest_Device_Name":"vdb",
"Bytes_Read":116524,
"Guest_IP_Address":"192.168.26.100"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想选择块设备bdev0,bdev1 ...以及它们的值通常这很容易使用vanilla javascript中的Object.keys但似乎我不能在角度使用此函数所以我尝试angular.forEach但它返回undefined.
这是我能走多远
function getData(){
$http.get(path)
.success(function(data){
$scope.devices = data
angular.forEach($scope.devices, function(item){
console.log(item['Block Devices']);
})
})
}
Run Code Online (Sandbox Code Playgroud)