我试图在控制器从模板获取的URL上运行XHR请求。
我得到这样的网址
角度代码:
ManageControllers.controller('ManageCtrl', ['$scope', '$http',
function ManageCtrl($scope, $http) {
$scope.urls = {
edit : '',
get : ''
};
$http.post($scope.urls.get).success(function(data) {
$scope.recipe = data;
});
}
]);
HTML模板:
<div ng-app="ManageApp">
<div ng-controller="ManageCtrl">
<input
ng-model="urls.save"
ng-init="
urls.save = '/save';
"
/>
<input
ng-model="urls.get"
ng-init="
urls.get = '/get';
"
/>
</div>
</div>
到目前为止,Angular从trmplate读取值。但看起来$ http在ng-init之前运行。所以我不知道用超时的某些函数运行$ http是否更好,或者有一些正确的,有角度的方式。
答案 0 :(得分:1)
您可以稍微更改代码而无需setTimeout:
<input
ng-model="urls.get"
ng-init="initGet('/get')"
/>
ManageControllers.controller('ManageCtrl', ['$scope', '$http',
function ManageCtrl($scope, $http) {
$scope.urls = {
edit : '',
get : ''
};
$scope.initGet = function(url) {
$scope.urls.get = url;
$http.post($scope.urls.get).success(function(data) {
$scope.recipe = data;
});
};
}
]);