I'm trying to add authentication capabilities to my app by providing a service auth
with login
and logout
methods and a loggedIn
property (once I get it to work, this will actually be the user object, but now it's just a boolean for simplicity) and a login
directive, which displays either an inline login form or a logout button, depending on the value of the loggedIn
variable.
Simple auth implementation:
app.factory 'auth', ($log) ->
{
loggedIn: false
login: (username, password) ->
$log.log "Login: #{username}, #{password}"
if username is "a" and password is "b"
@loggedIn = true
logout: ->
@loggedIn = false
}
(Obviously, once I get this to work, the login will be done through REST! This is just for convenience, and all the app logic resides on the server, which uses a traditional session)
I'm having trouble with the directive. I know that I can use template
or templateUrl
to choose a template statically, but how do I replace the current template with a new $compile
d one?
app.directive "login", ($compile) ->
restrict: "E"
templateUrl: "/login.html"
scope: { }
controller: ($scope, $element, $log, auth) ->
$scope.auth = auth
$scope.$watch 'auth.loggedIn', (newVal, oldVal) ->
$log.log "Done: #{newVal} <- #{oldVal}"
# here I should switch to the other template if newVal != oldVal
I know I could probably get the root element of the template, remove the old nodes and add the new ones, but I am hoping there is a more "declarative" way to do this in angular, without doing any DOM operations.