Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng单击函数立即执行函数而不单击_Javascript_Angularjs - Fatal编程技术网

Javascript ng单击函数立即执行函数而不单击

Javascript ng单击函数立即执行函数而不单击,javascript,angularjs,Javascript,Angularjs,我不熟悉angular,正在尝试设置登录系统。我有一些“按钮”设置,当点击按钮时,可以将用户重定向到Oauth提示符,指向用户facebook/google帐户。我的问题是,登录用户的功能在页面日志上立即执行,而不是在单击按钮时执行 我很确定根源在于JS对象的工作方式,但我仍在学习angularjs,这有点令人困惑 我相信将函数放在$scope上会立即执行它们,但我不知道如何将它们公开给ng click 有人能帮我想出如何使按钮按预期工作吗 模板: <ion-view title="Acc

我不熟悉angular,正在尝试设置登录系统。我有一些“按钮”设置,当点击按钮时,可以将用户重定向到Oauth提示符,指向用户facebook/google帐户。我的问题是,登录用户的功能在页面日志上立即执行,而不是在单击按钮时执行

我很确定根源在于JS对象的工作方式,但我仍在学习angularjs,这有点令人困惑

我相信将函数放在$scope上会立即执行它们,但我不知道如何将它们公开给ng click

有人能帮我想出如何使按钮按预期工作吗

模板:

<ion-view title="Account">
    <ion-nav-buttons side="right">
        <button menu-toggle="right" class="button button-icon icon ion-navicon"></button>
    </ion-nav-buttons>
    <ion-content class="has-header padding">

        <h1 ng-click="google_login()">Log in with Google</h1>
        <h1 ng-click="fb_login()">Log in with Facebook</h1>
        <h1 ng-click="dev_login()">Dev login</h1>
        <div id="logs"></div>
        <form class="list">
            <label class="item item-input">
                <input type="text" placeholder="Username" ng-model="user.username" required>
            </label>
            <label class="item item-input">
                <input type="password" placeholder="Password" ng-model="user.password" required>
            </label>
            <div class="padding">
                <button class="button button-block button-stable" ng-click="email_authenticate()">Login</button>
            </div>
        </form>
    </ion-content>
</ion-view>
这些在services.js中定义:

.factory('Authentication', function ($http) {
    return {
        authenticate: function () {
            return $http({
                url: 'https://api.squawkfinace.com/authenticate',
                method: 'post'//,
                //data: {facebook_authtoken: key}
            });
        },
        fb_authenticate: function () {
            return $.oauth2({
                 //Oauth details
            }, function (token, response) {
                localStorage.setItem("LoggedInAccount", JSON.stringify({'platform': 'facebook', 'token': token}));
                console.log(token);
                $state.transitionTo("app.notifications");
            }, function (error, response) {
                // do something with error object
            });
        },
        google_authenticate: function () {
            return $.oauth2({
                //oauth details
            }, function (token, response) {
                localStorage.setItem("Account", JSON.stringify({'platform': 'google', 'key': token}));

            }, function (error, response) {
                // do something with error object
                $("#logs").append("<p class='error'><b>error: </b>" + JSON.stringify(error) + "</p>");
                $("#logs").append("<p class='error'><b>response: </b>" + JSON.stringify(response) + "</p>");
            });
        },
        dev_authenticate: function () {
            return null;
        },
        email_authenticate: function () {
          return null;
        },
        logout: function () {
            localStorage.removeItem("Account");
            return null;
        }
    }
});
.factory('Authentication',函数($http){
返回{
身份验证:函数(){
返回$http({
网址:'https://api.squawkfinace.com/authenticate',
方法:“post”/,
//数据:{facebook_authtoken:key}
});
},
fb_身份验证:函数(){
返回$.oauth2({
//Oauth详细信息
},函数(令牌,响应){
setItem(“LoggedInAccount”,JSON.stringify({'platform':'facebook','token':token}));
console.log(令牌);
$state.transitiono(“应用程序通知”);
},函数(错误,响应){
//对错误对象执行某些操作
});
},
谷歌认证:函数(){
返回$.oauth2({
//oauth详细信息
},函数(令牌,响应){
setItem(“Account”,JSON.stringify({'platform':'google','key':token}));
},函数(错误,响应){
//对错误对象执行某些操作
$(“#日志”).append(

错误:“+JSON.stringify(error)+”

”; $(“#日志”).append(

响应:“+JSON.stringify(响应)+”

”; }); }, 开发人员身份验证:函数(){ 返回null; }, 电子邮件\u身份验证:函数(){ 返回null; }, 注销:函数(){ localStorage.removeItem(“帐户”); 返回null; } } });
这是因为您实际上正在执行函数

$scope.dev_login = Authentication.dev_authenticate();
应该是

$scope.dev_login = Authentication.dev_authenticate;
第一个场景执行函数并将结果分配给$scope。您希望将引用指定给函数

$scope.dev_login = Authentication.dev_authenticate;