根据具体值显示按钮

时间:2016-05-27 01:43:49

标签: javascript angularjs node.js strongloop

仅当登录用户的sendSMS属性值为“true”时,才显示该按钮。我将属性添加到以用户作为基本模型的查看器模型中。如果我想要ng-show =“(Viewer.sendSMS =='true')”,我无法显示按钮。我无法清楚地表达我的问题,但希望它可以理解。

控制器中的auth.js

    angular
  .module('app')
  .controller('AuthLoginController', ['$scope', 'AuthService', '$state',
      function($scope, AuthService, $state) {
    $scope.user = {
      email: '',
      password: ''
    };
    $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');
        });
    };
  }]);

服务中的auth.js

    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,
            sendSMS: sendSMS
          };
        });
    }
    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
    };
  }]);

创建样本-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: 'haykhk@sg.ibm.com', password: 'User0102', sendSMS: 'true'}
      ], cb);
    });
  }
};

report.html

<button type="button" ng-show="(Viewer.sendSMS == 'true')" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">
  • 这是我想使用sendSMS属性根据登录用户的属性设置值启用按钮的地方。

2 个答案:

答案 0 :(得分:0)

ng-show条件是在布尔值和字符串值之间进行松散比较,这会产生意外结果。考虑重写为:

<button type="button" ng-show="Viewer.sendSMS" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">

假设在范围内正确设置了Viewer个对象,则Viewer.sendSMStrue时会显示此按钮。

答案 1 :(得分:0)

只需删除true中的单引号。您不需要括号。

ng-show="(Viewer.sendSMS == true)"

OR

ng-show="Viewer.sendSMS == true"

OR

ng-show="Viewer.sendSMS"