Answer the question
In order to leave comments, you need to log in
Angular. How to pass a parameter to a function from a template and call get?
Hello.
What am I doing wrong?
.controller('ProfileDetail', function($scope, $http) {
$scope.myFunc = function (id) {
// получаем json
$scope.url = 'profile/?=' + id;
$http.get($scope.url).success(function (data) {
return data;
}).error(function (data, status) {
$scope.response = 'Request failed';
});
};
});
<div ng-controller="ProfileDetail">
<div>
{{ myFunc(4) }}
/div>
</div>
Answer the question
In order to leave comments, you need to log in
It's like that. The function will be executed. The truth will not output anything, but it will be executed.
Try codeo.me/5yQ
AND in HTML: codeo.me/5yR
Well, return a promise from your function that returns $http. In general, it’s better not to do this, but it’s better to explicitly explain the data and make requests to services, but for the purposes of training, you can turn a blind eye to this.
If I understand the original task correctly, it is better to do this:
angular.module('app')
.controller('ProfileDetail', $scope, $http) {
$scope.$watch('selectedId', function (id) {
if (!id) {
$scope.data = {};
return;
}
var url = 'profile/?=' + id;
$http.get(url).then(function (response) {
$scope.data = response.data;
}, function () {
// обрабатывайте ошибки
})
});
})
<div ng-controller="ProfileDetail">
<div>
{{ data }}
/div>
<span class="btn" ng-click="selectedId = 4"></span>
</div>
.controller('ProfileDetail', function($scope, $http) {
var url = 'profile/?=4';
$http.get(url).success(function (data) {
$scope.data = data;
}).error(function (data, status) {
$scope.response = 'Request failed';
});
});
<div ng-controller="ProfileDetail">
<div>
{{ data }}
/div>
</div>
.controller('ProfileDetail', function($scope, $http) {
$scope.data = '';
$scope.doThisShit = function(id){
var url = 'profile/?=' + id;
$http.get(url).success(function (data) {
$scope.data = data;
}).error(function (data, status) {
$scope.response = 'Request failed';
});
}
});
<div ng-controller="ProfileDetail">
<div ng-click="doThisShit(4)">
{{ data }}
/div>
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question