这是我的角度控制器:
angular
.module('hameedApp', [])
.controller('hameedController', function($scope, $http) {
$http({
method: 'GET',
url: 'json.php'
}).then(function(response) {
$scope.contacts = response.data;
}).catch(function(response) {
console.log('error');
})
});
这是我的body标签的html:
<table class="table">
<tr>
<th>Name</th>
<th>Phone No</th>
<th>Email</th>
</tr>
<tr ng-repeat="contact in contacts">
<td>{{contact.name}}</td>
<td>{{contact.gsm}}</td>
<td>{{contact.email}}</td>
</tr>
</table>
<script>
var test = {{contacts}}
console.log(test);
</script>
我想在一个名为“test”的变量中获得{{contacts}}
全数组json数据。
可能吗?
角度概念是你放在$scope
中的任何东西都无法在non-angular
代码中直接访问。您可以做的是,您可以使用window
对象将其分配给任何全局变量,如下所示。
angular
.module('hameedApp', [])
.controller('hameedController', function($scope, $http) {
$http({
method: 'GET',
url: 'json.php'
}).then(function(response) {
$scope.contacts = response.data;
window.contacts = response.data;
}).catch(function(response) {
console.log('error');
})
});
现在你可以在contacts
以及angular
代码中的任何地方访问non-angular
。
编辑
如果你想在contacts
代码中获得non-angular
,你必须使用callback
机制来告诉你的non-angular
代码有关更新的值。见下文
在你<head>
,在method
添加一个新的window
像这样
<head>
<script>
....
....
window.updateContacts = function(contacts) {
console.log(contacts);
}
....
....
</script>
</head>
现在你可以使用callback
代码中的angular
,如下所示
angular
.module('hameedApp', [])
.controller('hameedController', function($scope, $http) {
$http({
method: 'GET',
url: 'json.php'
}).then(function(response) {
$scope.contacts = response.data;
window.updateContacts(response.data);
}).catch(function(response) {
console.log('error');
})
});