javascript - 如何在 Ionic 中制作开/关按钮并执行功能?

标签 javascript angularjs ionic-framework

这里有一个很好的解决方案 How to make a On/Off button in Ionic

这是来自 Codepen 的演示

代码粘贴在这里:

angular.module('mySuperApp', ['ionic'])
.controller('MyCtrl',function($scope) {

      $scope.someFunction = function(){

               alert('I am pressed')

        }

});

<html ng-app="mySuperApp">
  <head>
    <meta charset="utf-8">
    <title>
      Toggle button
    </title>
     <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
          <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
        <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
          </head>
      <body class="padding" ng-controller="MyCtrl">
      <button class="button button-primary" ng-model="button" ng-click="button.clicked=!button.clicked" ng-class="button.clicked?'button-positive':'button-energized'">
          Confirm
        </button>
      </body>
    </html>

我的问题是如何在确认按钮的同一个 ng-click 事件上执行 someFunction() 可能像这样 ng-click="someFunction() && button.clicked =!button.clicked" ?

最佳答案

不使用 ; 分隔符,您可以将所有逻辑移动到 someFunction() 中,并将该函数设置为 ng-click 的表达式 属性:

angular.module('mySuperApp', ['ionic'])
  .controller('MyCtrl', function($scope) {

$scope.buttonOn = false;

$scope.someFunction = function() {
  $scope.buttonOn = (!$scope.buttonOn);
  alert('I am pressed');
}
  });
<html ng-app="mySuperApp">
  <head>
    <meta charset="utf-8">
    <title>
      Toggle button
    </title>
     <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
          <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
        <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
          </head>
      <body class="padding" ng-controller="MyCtrl">
        <button class="button button-primary" ng-click="someFunction()" ng-class="buttonOn?'button-positive':'button-energized'">
          Confirm
        </button>
      </body>
    </html>

关于javascript - 如何在 Ionic 中制作开/关按钮并执行功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35381246/

相关文章:

javascript - 将字符串转换为 html 元素以运行 javascript

javascript - 在react中使用map函数时如何使用if语句插入索引?

javascript - 私有(private)成员可以在 Angular 2 装饰器中访问

android - 如何创建一个可以订阅来自 drupal CMS 的推送通知的 Angular 应用程序?

android - Android 中的 ionic 5/电容器 ERR_CLEARTEXT_NOT_PERMITTED

javascript - 将变量传递到 Ionic Framework 页面(基于 AngularJS)

android - 空白页 Ionic 4 android 5.1

javascript - 从 colorbox 重定向到另一个页面

javascript - 如何解决 React 中的意外标记 (<) 错误?

javascript - 如何将 UTC 字符串转换为访问者本地时区的 momentJS?