2016-11-30 77 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