ng-click on a button to redirect WITHOUT controller

8,912 views
Skip to first unread message

Cory Svensson

unread,
Jul 24, 2014, 3:15:03 PM7/24/14
to ang...@googlegroups.com
Strange thing I'm trying to do apparently.

Using a button with onClick, I can do a simple Javascript redirect to a site.

Using a button with ng-click, I cannot do this same Javascript redirect to a site.

What I'm trying to accomplish is a button, which when clicked, will redirect to a URL which includes some two-way bind fields, but without the use of a custom function in a controller.

ex.

<button ng-click="window.open('http://www.site.com/login.jsp?username={{username}}&password={{password}}');">Log In!</button>


Is there any possible way to do this?  ng-click doesn't want to perform any variation of a redirect, whereas onClick doesn't want to display my two-way binding variables.

I have it working with a controller with a login() function which simply builds the URL, but I'd REALLY like to simply this and use angular's two-way binding without the controller.

Thanks!


Cory Svensson

unread,
Jul 24, 2014, 6:02:23 PM7/24/14
to ang...@googlegroups.com
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.

Is it a bug causing this to not work with a <button> tag?

Cory Svensson

unread,
Jul 24, 2014, 7:07:50 PM7/24/14
to ang...@googlegroups.com

Justin Russell

unread,
Jul 24, 2014, 9:33:33 PM7/24/14
to ang...@googlegroups.com
Angular expressions, like the code that goes {{here}} or ng-click="here" isn't evaluated in the same context as regular JavaScript code. In your case I think "window" is not defined and because Angular is forgiving of ReferenceErrors when evaluating expressions you never see an error generated. See the docs here: https://docs.angularjs.org/guide/expression

Also, If you stick with the "a" tag you may want to use ngHref instead of href: https://docs.angularjs.org/api/ng/directive/ngHref




----------------------------------------------------------------


On Thu, Jul 24, 2014 at 7:07 PM, Cory Svensson <cory.s...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Eric Eslinger

unread,
Jul 24, 2014, 9:49:01 PM7/24/14
to ang...@googlegroups.com
Yup, justin is right there - ng-click="window.bla" only works in a world where there's a controller that has a $scope.window = window (or better, $window) command. 

e
Reply all
Reply to author
Forward
0 new messages