2017-04-05 29 views

回答

1

你可以做這樣的事情:

for (var key in data) { 
    $scope[key] = p[key] 
} 
1

當然可以。獲取對象的鍵並指定創建範圍變量這樣

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 
var data = {item1: "Hello1",item2:"Hello2",item3:"Hello3",item4:"Hello4",item5:"Hello5"}; 
 

 
for(var k in data) { 
 
    $scope[k] = data[k] 
 
} 
 

 
console.log($scope.item1) 
 
console.log($scope.item2) 
 
console.log($scope.item3) 
 

 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
    
 
</div>

+0

感謝sachila ..但是你必須做出一個小的修正。 $ scope [k] = data [k]; –

+0

@I'mnidhin opps你是正確的更新它 –

相關問題