Javascript 如何将值从localStorage传递到输入字段

Javascript 如何将值从localStorage传递到输入字段,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我正在尝试将localStorage传递给输入字段,但我无法传递值,我不知道哪里出了问题,请帮助我,还有一件事我如何在控制器外部调用angularjs函数。谢谢你们的帮助 html 这可能很有用,将其存储在变量中并使用它,并且只使用id名称而不是密码 var x = document.getElementById("password").value = "Password"; alert ("The value was changed to: " + x); 这可能很有用,将其存储在变量中

我正在尝试将localStorage传递给输入字段,但我无法传递值,我不知道哪里出了问题,请帮助我,还有一件事我如何在控制器外部调用angularjs函数。谢谢你们的帮助

html


这可能很有用,将其存储在变量中并使用它,并且只使用id名称而不是密码

 var x = document.getElementById("password").value = "Password";
 alert ("The value was changed to: " + x);

这可能很有用,将其存储在变量中并使用它,并且只使用id名称而不是密码

 var x = document.getElementById("password").value = "Password";
 alert ("The value was changed to: " + x);

在Angular应用程序中,您应该在模块内编写所有代码。否则,就不会使用Angular的一个有用特性:模块化

例如:

app.factory("LocalStorage", function() {
    var LS = {};
    LS.getItem = function(key) {
        return localStorage[key];
    };
    LS.setItem = function(key, value) {
        localStorage[key] = value;
        return value;
    };

    return LS;
});
app.controller('loginctrl', function($scope, $http, LocalStorage) {
    $scope.email = LocalStorage.getItem('email');
    $scope.password = LocalStorage.getItem('password');

    $scope.login = function() {
        // code...
        // I'd rather to create another service "Api" or "Request", like this:
        return Api.doAuth($scope.login, $scope.password).then(function(res) {
            // For example, service `Request` write to `LocalStorage` status of current authentication.
            if(res) {
                myNavigator.pushPage('home.html');
            }
        });
    };
});
您不应该在外部应用程序中调用函数(从Angular的哲学观点来看,这是不正确的)。然而,有可能,你写对了:

// get our scope:
var scope = angular.element(document.getElementById('loginctrl')).scope();
// and call our function in the scope:
scope.login();
但这是Angular引导后需要执行的代码。 此外,您还可以使用Angular的服务执行任何功能

//find the `ng:app` element and get injector.
var injector = angular.element('body').injector();
//execute our own function with any services
injector.invoke(function($rootScope, $http, LocalStorage) {
    $http.get('/foo.php').then(function(res) {
        LocalStorage.setItem('answer', res);
    });
    $rootScope.bar = 1;

});

在Angular应用程序中,您应该在模块内编写所有代码。否则,就不会使用Angular的一个有用特性:模块化

例如:

app.factory("LocalStorage", function() {
    var LS = {};
    LS.getItem = function(key) {
        return localStorage[key];
    };
    LS.setItem = function(key, value) {
        localStorage[key] = value;
        return value;
    };

    return LS;
});
app.controller('loginctrl', function($scope, $http, LocalStorage) {
    $scope.email = LocalStorage.getItem('email');
    $scope.password = LocalStorage.getItem('password');

    $scope.login = function() {
        // code...
        // I'd rather to create another service "Api" or "Request", like this:
        return Api.doAuth($scope.login, $scope.password).then(function(res) {
            // For example, service `Request` write to `LocalStorage` status of current authentication.
            if(res) {
                myNavigator.pushPage('home.html');
            }
        });
    };
});
您不应该在外部应用程序中调用函数(从Angular的哲学观点来看,这是不正确的)。然而,有可能,你写对了:

// get our scope:
var scope = angular.element(document.getElementById('loginctrl')).scope();
// and call our function in the scope:
scope.login();
但这是Angular引导后需要执行的代码。 此外,您还可以使用Angular的服务执行任何功能

//find the `ng:app` element and get injector.
var injector = angular.element('body').injector();
//execute our own function with any services
injector.invoke(function($rootScope, $http, LocalStorage) {
    $http.get('/foo.php').then(function(res) {
        LocalStorage.setItem('answer', res);
    });
    $rootScope.bar = 1;

});

我想提请大家注意:angular引导后需要执行的
preauth
函数。如果我按照你说的去做,我想它会在控制器中执行
LocalStorage
函数,并且它会更改
$scope.email
$scope.password
值,每次调用控制器时,不是吗。如果这样做,它将始终显示登录错误empty@user2881430,不,请看。在重新加载页面后,localStorage实际上是未清除的。澄清?我可以在控件中使用
ng init
指令在DOM加载后调用我想引起注意:angular引导后需要执行的
preauth
函数。如果我按照你说的去做,我想它会在控制器中执行
LocalStorage
功能,并且每次调用控制器时都会更改
$scope.email
$scope.password
值,不是吗。如果这样做,它将始终显示登录错误empty@user2881430,不,请看。在重新加载页面后,localStorage实际上是未清除的。澄清?我能够在控制器中使用
nginit
指令在DOM加载后调用