2

This is my code

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
    <script>
        var myApp = angular.module('myApp', []);
        myApp.controller('myController', function ($scope) {

            $scope.getName = function () {
                $scope.name = fname;
                $scope.lastName = lastName;
            };

        })
    </script>
</head>
<body ng-app="myApp" >
    <div >
        <input type="text" ng-model="name"/> <br>
        <input type="text" ng-model="lastName" />
    </div>
    <div ng-controller="myController">
        <input type="button" value="click" ng-click="getName()" />
        <br>
        Hello FirstName {{name}}<br>
        Last Name {{lastName}}

</div>
</body>
</html>

values are coming while typing into textbox but i want values only after clicking the button.Please help me.
I tried in both ways simple as well as by using service or factory method but no success

1 Answer 1

1

You must separate the ng-models of the input texts and destination field. They are updating real-time because of having the same models.

I have created the correct version for you:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
    <script>
        var myApp = angular.module('myApp', []);
        myApp.controller('myController', function ($scope) {

            $scope.getName = function () {
                $scope.nameToSet = $scope.name;
                $scope.lastNameToSet = $scope.lastName;
            };

        })
    </script>
</head>
<body ng-app="myApp" >
    <div >
        <input type="text" ng-model="name"/> <br>
        <input type="text" ng-model="lastName" />
    </div>
    <div ng-controller="myController">
        <input type="button" value="click" ng-click="getName()" />
        <br>
        Hello FirstName {{nameToSet}}<br>
        Last Name {{lastNameToSet}}

</div>
</body>
</html>

UPDATE: Here is another version with using a factory:

    var myApp = angular.module('myApp', []);
    myApp.factory('container', function() {
        var model = {
          name: '',
          lastName: ''
        };
        return {
          model: model,
          setName: function(nameToSet) {
            model.name = nameToSet;
          },
          setLastName: function(lastNameToSet) {
            model.lastName = lastNameToSet;
          }
        };
    });
    myApp.controller('myController', function ($scope, container) {
        $scope.$watch('name', function(newvalue,oldvalue) {
          container.setName(newvalue);
        });
        $scope.$watch('lastName', function(newvalue,oldvalue) {
          container.setLastName(newvalue);
        });
        $scope.onNameType = function(value) {
          container.setName(value);
        };
        $scope.onLastNameType = function(value) {
          container.setLastName(value);
        };
        $scope.getName = function () {
            debugger;
            $scope.nameToSet = container.model.name;
            $scope.lastNameToSet = container.model.lastName;
        };
    })
    <!DOCTYPE html>
    <html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
    </head>
    <body ng-app="myApp" >
        <div >
            <input type="text" ng-model="name" ng-change="onNameType(name)"/> <br>
            <input type="text" ng-model="lastName" ng-change="onLastNameType(lastName)" />
        </div>
        <div ng-controller="myController">
            <input type="button" value="click" ng-click="getName()" />
            <br>
            Hello FirstName {{nameToSet}}<br>
            Last Name {{lastNameToSet}}

    </div>
    </body>
    </html>

Sign up to request clarification or add additional context in comments.

3 Comments

As I am new please guide me to how to achieve same thing using service or factory
Ok I will update the answer, please don't forget to mark as the correct answer
working for me, check the second sample, I see the same thing is provided when I click on the button?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.