angularjs - Append a popup to body on click with Angular (and then remove) -
i'm trying wrap head around directive concept in angular.
i want show modal box when clicking on link. contents of modal box dynamic. in jquery easy $("body").append(mymodal) , remove() dom when closed.
now i'd same in pure angular. have far:
a controller function:
$scope.userlogout = function() { notification.show(); }; a service:
.service('notification', ['$rootscope', function($rootscope) { var notification = { open: false, show : function() { this.open = true; }, hide: function() { this.open = false; } }; return notification; } ]) a directive:
.directive('notification', ['notification', function(notification){ return { restrict: 'e', replace: true, template: (notification.open) ? '<div class="mymodal"></div>' : '' } }]) how update directive when value in service changes? or right approach @ all?
for it's worth, angular, it's possible use data-ng-show , data-ng-hide on element styled modal. depending on use case, may not need create directive achieve want. consider following:
html:
... <div data-ng-show="notification.open" class="modalpopup"> ... {{notification.my_modal_message}} ... <button data-ng-click="closemodal()">close</button> </div> js (simplified):
function myctrl ($scope) { $scope.notification = { my_modal_message: "bender's back, baby!", open: false } $scope.logout = function () { // logout stuff logout().success(function () { // open modal $scope.notification.open = true; } } $scope.close = function () { $scope.notification.open = false; } } at times, it's better make full directive you. however, again - depending on use case - may need. keep in mind.
Comments
Post a Comment