【问题标题】:Displaying button according to specific value根据特定值显示按钮
【发布时间】:2016-05-27 11:58:46
【问题描述】:

我只想在登录用户的 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
    };
  }]);

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: '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 属性根据为登录用户设置的属性值启用按钮的地方。

【问题讨论】:

    标签: javascript angularjs node.js strongloop


    【解决方案1】:

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

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

    假设Viewer对象在作用域中被正确设置,当Viewer.sendSMStrue时,这个按钮应该出现。

    【讨论】:

      【解决方案2】:

      只需从true 中删除单引号即可。您不需要括号。

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

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

      ng-show="Viewer.sendSMS"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-27
        • 2014-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多