繁体   English   中英

根据属性值启用/禁用按钮

[英]Enabling/ disabling button base on values of a property

我仅在登录的用户具有sendSMS属性的值为true时才显示按钮。 为此,我应该在ng-show中放置什么? 另外,我不太确定是否正确放置了属性的值。 我创建了一个以用户为基本模型的Viewer模型,并添加了一个名为sendSMS的属性,以便可以使用此属性启用/禁用按钮。

create-sample-model.js

    var async = require('async');
module.exports = function(app) {
  //data sources
  var db = app.dataSources.db;
  //create all models
  async.parallel({
    viewers: async.apply(createViewers),
  }, function(err, results) {
    if (err) throw err;
  });
  //create reviewers
  function createViewers(cb) {
    db.automigrate('Viewer', function(err) {
      if (err) return cb(err);
      var Viewer = app.models.Viewer;
      Viewer.create([
        {email: 'example01@gmail.com', password: 'example123', sendSMS: 'false'},
        {email: 'example02@gmail.com', password: 'User01', sendSMS: 'true'}
      ], cb);
    });
  }
};

验证码

angular
  .module('app')
  .factory('AuthService', ['Viewer', '$q', '$rootScope', function(Viewer, $q,
      $rootScope) {
    function login(email, password, sendSMS) {
      return Viewer
        .login({email: email, password: password, sendSMS: sendSMS})
        .$promise
        .then(function(response) {
          $rootScope.currentUser = {
            id: response.user.id,
            tokenId: response.id,
            email: email
          };
        });
    }
    function logout() {
      return Viewer
       .logout()
       .$promise
       .then(function() {
         $rootScope.currentUser = null;
       });
    }

    function register(email, password, sendSMS) {
      return Viewer
        .create({
         email: email,
         password: password,
         sendSMS: sendSMS
       })
       .$promise;
    }

    return {
      login: login,
      logout: logout,
      register: register
    };
  }]);
  • 这是位于services文件夹中的身份验证

验证码

    angular
  .module('app')
  .controller('AuthLoginController', ['$scope', 'AuthService', '$state',
      function($scope, AuthService, $state) {
    $scope.Viewer = {
      email: '',
      password: '',
      sendSMS: ''
    };
    $scope.login = function() {
      AuthService.login($scope.user.email, $scope.user.password)
        .then(function() {
          $state.go('report');
        });
    };
  }])
  .controller('AuthLogoutController', ['$scope', 'AuthService', '$state',
      function($scope, AuthService, $state) {
    AuthService.logout()
      .then(function() {
        $state.go('home');
      });
  }])
    .controller('SignUpController', ['$scope', 'AuthService', '$state',
      function($scope, AuthService, $state) {
    $scope.user = {
      email: '',
      password: ''
    };

    $scope.register = function() {
      AuthService.register($scope.user.email, $scope.user.password)
        .then(function() {
          $state.transitionTo('report');
        });
    };
  }]);
  • 这是位于controllers文件夹中的身份验证

report.html

<button type="button" ng-show="currentUser.Viewer.sendSMS" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">
    Send sms for selected </button>

您可以在按钮上使用ng-disabled属性。 请参阅ng-disabled 可以根据sendSMS的作用域值禁用按钮

暂无
暂无

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

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