I'm trying to create an AngularJS directive to work with a modal window and I can't figure out how to isolate the scope. My goal is to have a modal window to add a new location. I want the directive to populate the modal with any needed data from the parent scope and handle the save button click. I got this working using the parent scope, but I have not been able to get it working with isolated scope.
The parent controller has a locations property, which in this case is the only parent property I need. The newLocation object should be isolated from the parent scope. When I add the isolated scope, the name input is not populated and the click handler doesn't fire. The scope declaration only allows @, =, and & properties so I can't augment it there, and adding properties in link has no effect.
Directive:
angular.module('pw').directive('addLocationModal', [
'appApi', function (appApi) {
return {
restrict: 'A',
scope: {
locations: '='
},
link: function (scope, element, attrs) {
scope.newLocation = {
name: 'xxx'
};
scope.saveLocation = function () {
alert('save location');
};
}
};
}
]);
Modal:
<div class="modal fade" id="add-location-modal" add-location-modal>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span>×</span>
</button>
<h4 class="modal-title">Add Location</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" ng-model="newLocation.name" required />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" ng-click="saveLocation()">Save</button>
</div>
</div>
</div>
angular-ui-bootstrap
. No need to re-invent the wheel – charlietfl