I cannot seem to figure out the issue here. On an ajax success response I set a value in the current controller that is not reflecting in the UI. The general answer I have found is to run with the Angular ajax functions and/or apply $apply or $digest to the $scope. None of these are working.
Please note in the code the {{ and }} angular tags are replaces with <% and %> as I'm using the blade tempting engine and these tags conflict.
The idea is to set a processing boolean in the controller. Set to true before ajax and the false after. The problem is the value is not returned to its false state. Running the $apply or $digest method both return Error: [$rootScope:inprog]
.
After the ajax I run
console.log($scope.processing);
console.log(this.processing);
console.log($scope);
Returning
undefind
undefind
And returns the $scope object.
However within the $scope object outputted in the console the value of processing is as it should be (false).
However it is not reflected in the UI it is still true. Clicking the toggle button sets the processing value to false and the UI is updated. So I'm super confused as to where the problem is...
HTML
<div class="panel panel-default" ng-controller="UnitOfMeasureController as uom">
<div class="panel-heading">
<h3 class="panel-title">Create new Unit of Measure</h3>
</div>
<div class="panel-body">
<div ng-hide="uom.processing">
<form ng-submit="uom.processForm()" id="new_uom_form">
<div class="form-group">
<label for="uom_input01">Name</label>
<input ng-model="uom.formData['name']" type="text" class="form-control" id="uom_input01" placeholder="" name="uom[input01]" value="{{\xsds::old('uom.input01',$values)}}">
</div>
<div style="text-align:right"><button type="submit" class="btn btn-primary" ><i class="fa fa-plus-square"></i> Create new Unit of Measure</button></div>
</form>
</div>
{!!\xsds::angularLoader('ng-show="uom.processing"')!!}
</div>
<button ng-click="uom.processing = false">Toggle</button>
<%uom.processing%>
</div>
app.js
(function( ){
var app = angular.module('ingredients',[], function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
app.controller('UnitOfMeasureController', ['$scope','$http', function($scope,$http) {
formData = [];
this.processing = false;
this.processForm = function( ){
this.processing = true;
$http.get(document.js_root+'/recipe/ingredients/uom/ajax-create').
success(function(data, status, headers, config) {
/* $scope.$apply(function(){
$scope.processing = false;
});*/
console.log($scope.processing);
console.log(this.processing);
console.log($scope);
$scope.processing = false;
if (!data.success) {
console.log(data.errors);
} else {
console.log('success');
}
//$scope.$digest();
//$scope.$apply(); similar but slower
/* $scope.$apply(function() {
$scope.processing = false;
});*/
}).
error(function(data, status, headers, config) {
$scope.processing = false;
if(document.is_js_dev){
alert(status+' ');
}
});
return false;
};
}]);
})();