0

I am trying to do simple programming in angularjs. I have created two buttons. The first button has text "clickHere", and the second has no text. My task is when I click the button that has text, it will automatically get empty and the second button will have the same text. (and vice versa).

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

function MyCtrl($scope) {
    $scope.name = 'Superhero';
    $scope.name1 = '';
    var val = 'Hello';
    $scope.click0 = function() {
        //alert("hello!");
        $scope.name = $scope.name1;
        $scope.name1="";
        //return val;
    };
    $scope.click1 = function() {
        //alert("hello!");
        $scope.name1 = $scope.name;
        $scope.name="";
//        return val;
    };
}
<body ng-app="myApp">
<div ng-controller="MyCtrl">
    <button ng-click="click0()" >{{name}}</button>
    <button ng-click="click1()" >{{name1}}</button>
</div>
</body>

Here is my code in jsfiddle. http://jsfiddle.net/simpleorchid/a65zV/

1
  • What doesn't work? I've tried the fiddle and don't see the problem. Commented Jul 30, 2014 at 3:24

2 Answers 2

1

I think your issue was you were overwriting your stored value. I added a $scope variable called word, and now it works:

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

function MyCtrl($scope) {
    $scope.word = 'Superhero';
    $scope.name = $scope.word;
    $scope.name1 = '';

    $scope.click0 = function() {
        $scope.name1 = $scope.word;
        $scope.name="";
    };
    $scope.click1 = function() {
        $scope.name = $scope.word;
        $scope.name1="";
    };

}

Udpated Fiddle: http://jsfiddle.net/pWjLR/1/

0
0

Just replace the click1() with click0() like in the JSFiddle here and everything will work just as you want it to.

0

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.