I could not get text field value present inside the modal pop up using Angular.js. I am providing my code below.
view.html:
<modal title="Driver Information" visible="showModal">
<div class="col-md-12">
<div class="form-group">
<label>Comment</label>
<textarea class="form-control" id="comment" rows="3" ng-model="comment"></textarea>
</div>
</div>
<button class="btn btn-success" id="singlebutton" ng-click="save();">Save</button>
<div style="clear:both;"></div>
</modal>
My controller page is given below.
viewcontroller.js:
var dashboard=angular.module('easyride');
dashboard.controller('viewcontroller',function($scope){
$scope.save=function(){
console.log('comment',$scope.comment);
}
})
dashboard.directive('modal', function () {
return {
template: '<div class="modal fade">' +
'<div class="modal-dialog modal-lg">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h4 class="modal-title">{{ title }}</h4>' +
'</div>' +
'<div class="modal-body" ng-transclude></div>' +
'</div>' +
'</div>' +
'</div>',
restrict: 'E',
transclude: true,
replace:true,
scope:true,
link: function postLink(scope, element, attrs) {
scope.title = attrs.title;
scope.$watch(attrs.visible, function(value){
if(value == true)
$(element).modal('show');
else
$(element).modal('hide');
});
$(element).on('shown.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = true;
});
});
$(element).on('hidden.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = false;
});
});
}
};
});
In the console message I am getting the blank value even I am entering some value in comment field and click on save button. Here I need to get the user typed the comment value after click on save button and once the value will be printed via console message the pop up should close.
scope:falseand check if it works that way?scope:true.$scope.commentexists in the directive scope, but it is not the same as$scope.commentof the controller's scope. Use dot in your model or bindings to pass the callback into your directive.