Answer the question
In order to leave comments, you need to log in
Working with angular, bootstrap modal windows, how to work with AJAX correctly?
Today I poked around angular and bootstrap to refactor my crutch to jquery.
Window button
<div ng-app="app" ng-controller="Ctrl">
<button ng-click="open('view.html')">Open dialog</button>
</div>
angular.module('app', ['ui.bootstrap.dialog'])
.controller('Ctrl', function($scope, $dialog, $window) {
$scope.open = function(templateUrl) {
var options = {
backdrop: true,
keyboard: true,
controller: 'DialogCtrl',
templateUrl: templateUrl
};
var dialog = $dialog.dialog(options);
dialog.open().then(function(result) {
if (result === 1) {
$window.alert("OK pressed");
}
});
};
})
.controller('DialogCtrl', function($scope, dialog) {
$scope.close = function(result) {
dialog.close(result);
};
});
// Assign handlers immediately after making the request,
// and remember the jqXHR object for this request
var jqxhr = $.ajax({
type: 'POST',
url: '/backend/controller.php',
data: {'action':'editCoord', 'coordId':coordId, 'x':x, 'y':y, 'fieldName':fieldName}
}).success(function(responce) {
var checkResponce = jQuery.parseJSON( responce );
// Возникла ошибка
if (typeof checkResponce.error !='undefined') {
$('.error').html(checkResponce.error).show(300);
}else {
// Жадный рендер
renderAll(false,responce);
// Закрыть все окна
$.arcticmodal('close');
}
})
.fail(function() {
console.log( "error" );
})
.done(function() {
$('.load').hide();
})
Answer the question
In order to leave comments, you need to log in
you have replaced caste with jquery guano with angular.js.
Never work with jquery or dom in a controller. There are directives for this.
Never work with $http in the controller, take it all into services.
The construction you provide is implemented approximately like this:
angular.module('app')
.factory('myApiClient', function ($http, $q) {
return {
save: function (data) {
return $http({
method: 'POST',
url: 'some_url',
data: data
}).then(function (response) {
if (!response.data || response.data.error) {
return $q.reject();
}
return response.data; // я не уверен надо ли вам это... сами решите
}, function () {
return $q.reject();
});
}
}
})
.controller('MyCtrl', function ($scope, myApiClient) {
$scope.save = function () {
myApiClient.save(...).then(function (data) {
$scope.showResponse = true;
}, function () {
$scope.showResponse = false;
})
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question