我在角度控制器中有这个功能:
$scope.get_all_places = function () {
$http.get("get-all-places").then(function (response) {
$scope.selected_place = eval(response.data[0]);
});
};
我希望将$scope.selected_place
的值设置为此JavaScript代码:
var $shape_options = selected_place.shape_attributes;
var $map_options = selected_place.map_attributes;
switch ($shape_options.shape_type) {
case 'circle':
var $center = selected_place.center;
var $radius = selected_place.radius;
break;
case 'rectangle':
var $bounds = selected_place.rectangle;
break;
case 'polygon':
var $path = selected_place.polygon;
break;
}
但是在角度之前执行的JavaScript代码从服务器获取数据的问题。
任何想法怎么做?
答案 0 :(得分:4)
将所有代码放在ajax调用的print_r($category_1['children']);
函数中,ajax以异步方式执行
then
答案 1 :(得分:2)
您可以创建一个功能:
function determineShape(selected_plane){
var $shape_options = selected_place.shape_attributes;
var $map_options = selected_place.map_attributes;
switch ($shape_options.shape_type) {
case 'circle':
var $center = selected_place.center;
var $radius = selected_place.radius;
break;
case 'rectangle':
var $bounds = selected_place.rectangle;
break;
case 'polygon':
var $path = selected_place.polygon;
break;
}
}
然后在promise响应中调用它:
$scope.get_all_places = function () {
$http.get("get-all-places").then(function (response) {
// Run after promise is returned
determineShape(response.data[0]);
});
};
答案 2 :(得分:1)
在jQuery中获取变量:
试试这个
var selected_place = $('[ng-controller="your controller name here"]').scope().selected_place;
小错误:
再试一次:
var selected_place = $('[ng-controller="your controller name here"]').scope().get_all_places;
答案 3 :(得分:0)
不是最整洁的解决方案,但您可以将值放入hidden
输入字段,然后将其设置为JS变量,例如:
<input id="selected_place" type="hidden" value="{{ selected_place }}"/>
......和JS
document.getElementById("selected_place").value;