如何使用NG-点击重定向如何使用、重定向、NG

2023-09-13 02:47:44 作者:盾破千軍

超简单AngularJS的应用程序,我试图建立,接受证书分为两个文本框,然后使用双向绑定到一个按钮点击一个网址,包括其中的两个变量重定向。

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=...>

(或者NG-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?

**编辑:这混淆我的部分,这个工作(只是没有双向绑定工作):

** The part that confuses me is, this works (just without two-way binding working):

<button onClick="window.open('http://www.example.com/{{username}}');">

我预计,改变的onClick到NG-点击会给我同样的行为,但具有双向约束力。

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.

我不知道为什么按钮标签将无法正常工作给予这种行为,如上所述,但这里的工作code。

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.

推荐答案

将一个方法控制器上做重定向并有所谓的形式 NG-点击上你的按钮。

Put a method on your controller to do the redirect and have that called form the ng-click on your button.

标记:

<button ng-click="goLogin()">Log in</button>

控制器:

.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);
    };
})

另外还要注意要拨打 $ location.url()不是路径()

OR ...

添加 $位置来你的范围,并调用 URL({{NEWURL}})

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.