简体   繁体   English

需要简单的angularjs编程帮助

[英]Need help with simple angularjs programming

I am trying to do simple programming in angularjs. 我正在尝试在angularjs中进行简单的编程。 I have created two buttons. 我创建了两个按钮。 The first button has text "clickHere", and the second has no text. 第一个按钮带有文本“ clickHere”,第二个按钮没有文本。 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. 这是我在jsfiddle中的代码。 http://jsfiddle.net/simpleorchid/a65zV/ http://jsfiddle.net/simpleorchid/a65zV/

I think your issue was you were overwriting your stored value. 我认为您的问题是您正在覆盖储值。 I added a $scope variable called word , and now it works: 我添加了一个名为word$scope变量,现在它可以工作:

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/ 加盖的小提琴:http://jsfiddle.net/pWjLR/1/

只需更换click1()click0()喜欢在这里的jsfiddle ,一切都将工作一样,你想让它。

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

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