admin管理员组

文章数量:1324904

I am trying to create simple web page using angular. I have 2 textbox and also 2 buttons. 1 button to set value prdefine in textbox 2 write some text and add. When i will click on add button then textbox 1and 2 should printon screen another place.

Here the code. index.html

<!DOCTYPE html>
<html lang="en" ng-app="chatApp">
<head>
<meta charset="UTF-8">
<title>Chat Application</title>
<script src="scripts/angular.min.js"></script>
<script src="scripts/app.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<script>
function myFunction() {
document.getElementById("Text1").value = "Johnny Bravo";
}
</script>
<body ng-controller="chatController">
<div class="container">
    <div class="chatArena">

        <div class="userList">
            <ul>
                <li ng-repeat="person in listPeopleAdded" >{{person.name}}</li>
                <li ng-repeat="person in pretext" >{{person.name}}</li>

            </ul>
        </div>
    </div>
    <div class="chatAdder">
        <input type='text' ng-model='textbox' name='textbox1' id='Text1'>
        <button onclick="myFunction()" ng-click="pset(textbox)">set value</button>
        <br>
        <br>

        <input type="text" name="personName" class="chatInputPersonName" ng-model="personName">
        <button ng-click="addPerson()">Add Value</button>
    </div>
    </div>
    </body>
    </html>

app.js

var app = angular.module('chatApp',[]);

 app.controller('chatController',function($scope){

$scope.pretext=[];
$scope.listPeopleAdded = [];
$scope.addPerson = function(){
    $scope.listPeopleAdded.push({name:$scope.personName})

}
$scope.pset = function(textbox){
    alert($scope.textbox)
    $scope.pretext.push({name:$scope.textbox})
}

});

in app.js alert($scope.textbox) show undefined value.

I am trying to create simple web page using angular. I have 2 textbox and also 2 buttons. 1 button to set value prdefine in textbox 2 write some text and add. When i will click on add button then textbox 1and 2 should printon screen another place.

Here the code. index.html

<!DOCTYPE html>
<html lang="en" ng-app="chatApp">
<head>
<meta charset="UTF-8">
<title>Chat Application</title>
<script src="scripts/angular.min.js"></script>
<script src="scripts/app.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<script>
function myFunction() {
document.getElementById("Text1").value = "Johnny Bravo";
}
</script>
<body ng-controller="chatController">
<div class="container">
    <div class="chatArena">

        <div class="userList">
            <ul>
                <li ng-repeat="person in listPeopleAdded" >{{person.name}}</li>
                <li ng-repeat="person in pretext" >{{person.name}}</li>

            </ul>
        </div>
    </div>
    <div class="chatAdder">
        <input type='text' ng-model='textbox' name='textbox1' id='Text1'>
        <button onclick="myFunction()" ng-click="pset(textbox)">set value</button>
        <br>
        <br>

        <input type="text" name="personName" class="chatInputPersonName" ng-model="personName">
        <button ng-click="addPerson()">Add Value</button>
    </div>
    </div>
    </body>
    </html>

app.js

var app = angular.module('chatApp',[]);

 app.controller('chatController',function($scope){

$scope.pretext=[];
$scope.listPeopleAdded = [];
$scope.addPerson = function(){
    $scope.listPeopleAdded.push({name:$scope.personName})

}
$scope.pset = function(textbox){
    alert($scope.textbox)
    $scope.pretext.push({name:$scope.textbox})
}

});

in app.js alert($scope.textbox) show undefined value.

Share Improve this question asked Aug 3, 2016 at 6:27 Mehandi HassanMehandi Hassan 2,6114 gold badges17 silver badges20 bronze badges 1
  • Use only ngClick. Also you don't need to pass it as parameter. – developer033 Commented Aug 3, 2016 at 6:28
Add a ment  | 

2 Answers 2

Reset to default 2

Try this , its working You can use angular.element(document.getElementById("Text1")) to get value from javascript. And this value set to scope variable.

No need to send any value to function since textbox is scope value.

  $scope.pset = function(){
    var   x=angular.element(document.getElementById("Text1"));      
    $scope.textbox = x.val();
    alert($scope.textbox);
  }

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
 
  $scope.pset = function(){
     var   x=angular.element(document.getElementById("Text1"));      
    $scope.textbox = x.val();
    alert($scope.textbox);
  }
});
<script data-semver="1.4.9" src="https://code.angularjs/1.4.9/angular.js" data-require="[email protected]"></script>
 <script>
   window.onload=function(){
  // document.getElementById("Text1").value = "Johnny Bravo";
  
   }
   function myFunction() {
document.getElementById("Text1").value = "Johnny Bravo";
}
   
 </script>  

<body ng-app="plunker" ng-controller="MainCtrl">
 <input type='text' ng-model='textbox' name='textbox1' id='Text1'>
 <button onclick="myFunction()" ng-click="pset()">set value</button>
  </body>

    View page:

    <div class="col-md-6 col-md-offset-3">
        <h2>Login</h2>
        <form name="form" ng-submit="vm.login()" role="form">
            <div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
                <label for="username">Username</label>
                <input type="text" name="username" id="username" class="form-control" ng-model="vm.username" required />
                <span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
            </div>
            <div class="form-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
                <label for="password">Password</label>
                <input type="password" name="password" id="password" class="form-control" ng-model="vm.password" required />
                <span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
            </div>
            <div class="form-actions">
                <button type="submit" ng-disabled="form.$invalid || vm.dataLoading" class="btn btn-primary">Login</button>
                <a href="#!/register" class="btn btn-link">Register</a>
            </div>
        </form>
    </div>

In controller :
(function () {
    'use strict';

    angular
        .module('app')
        .controller('LoginController', LoginController);

    LoginController.$inject = ['$location', 'AuthenticationService', 'FlashService'];
    function LoginController($location, AuthenticationService, FlashService) {
        var vm = this;

        vm.login = login;

        function login() {
            vm.dataLoading = true;
            AuthenticationService.Login(vm.username, vm.password, function (response) {
                if (response.success) {
                    AuthenticationService.SetCredentials(vm.username, vm.password);
                    $location.path('/');
                } else {
                    FlashService.Error(response.message);
                    vm.dataLoading = false;
                }
            });
        };
    }

})();

本文标签: javascriptHow do i get textbox value in angularjsStack Overflow