2016-05-27 3 views
1

Я хочу отобразить кнопку, только если пользователь, который входит в систему, имеет значение «true» для свойства sendSMS. Я добавил свойство в модель зрителя, в которой пользователь имеет базовую модель. Я не могу отобразить кнопку, если я хочу 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: '[email protected]', password: 'example123', sendSMS: 'false'}, 
     {email: '[email protected]', 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, чтобы включить кнопку в соответствии со значением, установленным для свойства для зарегистрированного пользователя.

ответ

0

Условие ng-show делает свободное сравнение между логическим значением и строковым значением, которое будет иметь неожиданные результаты. Рассмотрим переписывание как:

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

Предполагая, что Viewer объект быть установлен должным образом в объеме, эта кнопка должна появиться, когда Viewer.sendSMS является true.

0

Просто удалите одинарную цитату из true. Вам не нужны скобки.

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

ИЛИ

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

ИЛИ

ng-show="Viewer.sendSMS"

Смежные вопросы