代码之家  ›  专栏  ›  技术社区  ›  Niraj

单击时角度更改按钮文本

  •  -1
  • Niraj  · 技术社区  · 7 年前

    我正在做一个游戏,我需要让用户在完成前一个关卡后选择下一个关卡。这可以上升到第7级。

    我需要能够改变按钮上的文字,以表明下一个标签号高达7。

    所以,在控制器中,我需要读取之前显示的按钮文本,以便显示下一个数字。

    有人能帮忙吗?这是我的密码笔 codepen link

    以及迄今为止我尝试过的附带代码

    <html ng-app="myApp">
      <body ng-controller="myCtrl">
         <div id="nextlevel">
            <h3>Level Completed!</h3>
            <button id="nextplay" ng-model="number" ng-init="buttontext='Level 
            2'">{{buttontext}}</button>
       </div>    
      </body>
    </html>
    

    和管制员-

    myApp = angular.module("myApp",[]);
    
    myApp.controller("myCtrl", function($scope){
    
      document.getElementById("nextplay").addEventListener('click', function() {
            nextlevel.style.display = 'none';
            setTimeout(function() {
              nextlevel.style.display = '';
            }, 500);
    
            if ($scope.number <=7) {            
               $scope.number = $scope.number + 1;
            }
            el.emit('gameStarted', {});        
        });
      $scope.buttontext = "Level " + $scope.number;
    });
    

    我不知道如何将按钮文本的值更新为下一个值。

    1 回复  |  直到 7 年前
        1
  •  1
  •   amir.algazi    7 年前

    以下是我建议注意的解决方案和注意事项:

     <html ng-app="myApp">
      <body ng-controller="myCtrl">
       <div id="nextlevel">
         <h3>Level Completed!</h3>
         <button id="nextplay" ng-click="buttonClicked()">
          Level {{number}}
         </button>
       </div>
      </body>
     </html>
    
    
      myApp = angular.module("myApp",[]);
    
      myApp.controller("myCtrl", function($scope) {
      $scope.number = 1;
    
      $scope.buttonClicked = function() {
        nextlevel.style.display = 'none';
        setTimeout(function() {
          nextlevel.style.display = '';
        }, 500);
        $scope.number++;
      }
     });
    
    • 在AngularJS中,有一个名为“ng click”的指令,它调用控制器$scope中包含的函数,而不是“addEventListener”。

    • 级别可以直接在HTML中写入,绑定到$scope变量(称为“数字”)。

    • 这里不需要指令“ng init”。

    推荐文章