Reputation: 125
Super-simple AngularJS app I'm trying to build, to accept credentials into two text boxes, then use two-way binding to redirect on a button click to a url which includes the two variables in it.
My problem is, I can get it working for a simple
<a href=...>
(or maybe ng-href=...) but for some reason, no matter what I do, I can't get a redirect using a
<button>
I've tried a lot of variations, but here's what I'm trying to do
<button ng-click="$location.path('http://example.com/login.jsp?un={{username}}&pw={{password}}')" class="btn btn-lg btn-warning">Log In!</button>
I CAN get it working if I call a function in a controller, but this is such a simple thing, I'd like to get it done on the page if possible.
Also, as a side-question, what are the security concerns of logging into a site like this?
** Edit: The part that confuses me is, this works (just without two-way binding working):
<button onClick="window.open('http://www.example.com/{{username}}');">
I'd expect that changing onClick to ng-click would give me the same behaviour, but with two-way binding.
**Re-Edit / Solution Alright, so I finally got a workable solution.
I have NO idea why the Button tag won't work to give this behaviour, as stated above, but here's the working code.
<a href="https://www.example.com/login.jsp?un={{username}}&pw={{password}}" class="btn btn-lg btn-warning">Log In!</a>
By giving it the same class as I intended to use for the button, the text shows up looking like a button, just the same.
Upvotes: 11
Views: 105324
Reputation: 9
This should definitely work
$scope.loadSchedules = function () {
window.location.href = '/Admin/Admin/DoctorsSchedule';
};
**Define a function inside "ng-click" attribute**
<button type="button" class="btn btn-primary success" ng-click="loadSchedules()">Ok</button>
Upvotes: 0
Reputation: 271
you can also use ng-href.
<button ng-href="http://example.com/login.jsp?un={{username}}&pw={{password}}" class="btn btn-lg btn-warning">Log In!</button>
Upvotes: 6
Reputation: 12186
Few things to note here:
$window.location.href = '/url'
on an inline ng-click
expression will not work.$location
is not exposed automatically to your scope, you'll need to expose this to the view by injecting it first to the controller and then the scope.
.controller('LoginCtrl', function($scope, $location) { $scope.$loc = $location; })
It's worth noting that $location.path()
& $location.url()
will NOT redirect you to a new page, this is used for routing so that route callbacks/watchers can do it's thing. The $location service allows you to change only the URL; it does not allow you to reload the page.
The best (and also angular way imo) is to add a method to your scope in the controller. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API: $window.location.href
.
Use a method bound to your controller:
$scope.redirect = function(url, refresh) {
if(refresh || $scope.$$phase) {
$window.location.href = url;
} else {
$location.path(url);
$scope.$apply();
}
}
And then call your method from your ng-click
expression:
<button ng-click="redirect('/')">GO HOME! NO BODY LOVES YOU</button>
Note: The above method will require you to inject both $location
& $window
to your controller.
Upvotes: 11
Reputation: 16907
Put a method on your controller to do the redirect and have that called form the ng-click
on your button.
Markup:
<button ng-click="goLogin()">Log in</button>
Controller:
.controller('LoginCtrl', function($scope, $location) {
$scope.form = {
username: null,
password: null
};
$scope.goLogin = function() {
$location.url('http://test.com/login.jsp?un='+ $scope.form.username +'&pw="+ $scope.form.password);
};
})
Also note you want to call $location.url()
not path()
OR...
Add $location
to your scope and call url({{newUrl}})
:
$controller('MyCtrl', function($scope, $location) {
$scope.$location = $location;
})
I'd still go with calling method on the scope.
Upvotes: 15