相关疑难解决方法(0)

指令之间的AngularJS通信

我是Angular.js的新手,我需要在我的应用程序之间进行指令之间的一些通信,我阅读了一些关于链接和需求的文档,但是无法准确理解它是如何工作的.

我有一个简单的例子:活小提琴:http://jsfiddle.net/yw235n98/5/

  • 2个指令:firstDir,secondDir ::带有一些数据
  • firstDir有一个点击功能,可以改变数据值
  • 当firsDir点击功能被触发时我想改变secondDir中的数据.

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)

javascript angularjs angularjs-directive

25
推荐指数
2
解决办法
2万
查看次数

如何使用角度js对JSON对象进行交互

我有一个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)

javascript json angularjs

1
推荐指数
1
解决办法
90
查看次数

标签 统计

angularjs ×2

javascript ×2

angularjs-directive ×1

json ×1