2016-11-30 25 views
0

我在我的angualr js文件中有一个数组对象,它试图控制台在js文件中显示结果。当我试图在html文件中使用同一个对象时,它显示为空。数组对象在显示为空的同时读取子页面时显示为空html页面 - angular JS

这是到目前为止我的代码:它打印为{}

任何帮助,说我要去哪里错了对话框上

$scope.testarray=[]; 
$rootScope.$on('rootScope:testarray', function() { 
     $scope.testarray = UserDataService.testarray;//this is read from a api service 
     console.log("test"+ $scope.testarray) 
//prints on the console as :: test[object Object] 
$scope.showdialog('testdialog.html'); 
     }); 

$scope.showdialog= function (id) { 

     $mdDialog.show({ 
      scope: $scope.$new(), 
      templateUrl: id 
     }); 
    } 

testdialog.html

<div ng-controller="testCtrl"> <!-- same controller as the above function!--> 

    <div > 
    <div layout="column" layout-padding flex> 

    <b> <span>{{ testarray}}</span></b> 


</div> 

    </div> 
    </div> 

。我没有看到使用问题。

+0

u能提供样本数据的plunker –

回答

0

你可能有不同的$scope内部对话这是不是控制器控股testarray

mddialouge的文档的$scope有一些选项叫做locals: { employee: $scope.userName }

使用locals添加在对话框中使用的本地值。

$mdDialog.show({ 
      controller: 'DialogController', 
      locals: {testarray : $scope.testarray} 
      templateUrl: id 
     }); 

尝试添加该对话框注入locals到控制器。

angular 
.module('yourApp') 
.controller('DialogController', function ($scope, testarray) { 
    // items is injected in the controller, not its scope! 
    $scope.testarray = testarray; 
}); 
+0

我的问题不是在js文件或显示对话框,当我试图安慰任何地方在JS它打印正确的对象。它只在js中显示为空。我厌倦了你的选择也没有work.Any不同的方式来使用它在HTML? – user168983

+0

当地人可用于为mdDialog设置的控制器中的“DI”。获取并将其添加到'$ scope'。 – sabithpocker

+0

无论如何,这个数组在rootcope中,从html工作中删除控制器指令。无论如何 – user168983