简体   繁体   English

如何在按钮单击时从文本字段将值设置为$ rootScope

[英]How to set value to $rootScope from text field on button click

Following is what I want to do, in JavaScript style. 以下是我想以JavaScript样式进行的操作。 At loading my html div (token) should show what ever value in $rootScope.token .Then when ever the button is pressed after typing text in input field (tokenInp), i need to update the $rootScope.token and also the displayed value in HTML. 在加载我的html div(token)时应该显示$rootScope.token值。然后在输入字段(tokenInp)中键入文本后每当按下按钮时,我需要更新$rootScope.token以及显示的值在HTML中。

document.getElementById("token").innerHTML = $rootScope.token;

function setToken(){
    $rootScope.token = document.getElementById("tokenInp").value;
    document.getElementById("token").innerHTML = $rootScope.token;
}

How to do this using AngularJS (im really new to it) 如何使用AngularJS做到这一点(真的很新)

In your controller create a empty object and a method for setting the value 在您的控制器中创建一个空对象和一种设置值的方法

var token = {};

$scope.setToken = function(){
   $rootScope.token = token;
};

And in your html bind values to your textbox 并在您的html中将值绑定到文本框

<input type='text' ng-model='token' ng-click='setToken()'> </input>

In you Html 在你的HTML

    <span ng-bind="token"></span>
   <input type='text' ng-model='tokenValue'> </input>
<input type="button" ng-click='modifiedTokenValue ()'/>

In you controller 在您的控制器中

 $scope.token = $rootScope.token;

 $scope.modifiedTokenValue = function(){
   $rootScope.token = $scope.tokenValue;
   $scope.token = $scope.tokenValue;
 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM