I wrote a bootstrap3 modal for my project before learning angular which was triggered using a button. Now I want to toggle it using angular variable. Taking reference from this article, i have simplified my code as follows:
My modal html:
<div id="registered" tabindex="-1" modal-toggle role="dialog" aria-labelledby="registeredModal" aria-hidden="true" class="modal fade">
<div class="modal-dialog modal-lg">
...
...
</div>
</div>
modalToggle directive:
app.directive("modalToggle",function(){
return function(scope, element, attrs){
scope.$watch(scope.loaded.showModal, function(value) {
if (value) element.modal('show');
else element.modal('hide');
});
}
})
scope.loaded.showModal is set on following controller, which is called after i hit tab on my form input:
app.controller('validatectrl',[ '$http', '$scope', '$upload', $location, function($http, $scope, $location){
unique: function(param){
$scope.loading={};
$scope.loaded={};
$scope.loading[param.field]=true;
var currentPath=$location.path();
var webCall = $http({
method: 'POST',
url: currentPath+'/validation',
async : true,
headers: {
'Content-Type': 'application/json'
},
timeout:10000,
data: param});
webCall.then(handleSuccess,handleError);
function handleSuccess(response) {
...
if(response.data.status===1) {
...
}
else if(response.data.status===0){
$scope.loaded["showModal"]=true;
alert("duplicate item");
}
}
function handleError(response){
$scope.loaded[param.field]={};
$scope.loading[param.field]=false;
$scope.loaded[param.field]["error"]=true;
$scope.loaded[param.field]["Message"]="Cannot fetch data from server";
};
}
Everything else works except for the modal does not pop up.