0

So I have a dataservice function that with an api call get's the type of the logged in account(admin and customer are two of them).

If a customer is logged in, five columns shouldn't be shown in a table I have with data. So I use an ng-if which doesn't seem to work.

this.hideSensitiveData = true;
function updateHideSensitiveDataFlag() {
        return dataservice
           .getUserType()
           .then(function (data) {
               var userType = data;
               this.hideSensitiveData = userType === "Customer";
           });
    }

And one of the coulmns(in thead and tbody):

<th sort by="'pumpPressure'" order="order" reverse="reverse" ng-if="this.hideSensitiveData">{{ 'PumpPressure' | translate}}</th>

<td ng-if="this.hideSensitiveData">{{sequence.pumpPressure.toFixed(1)}}</td>

"data" here will be the account type. So if I understand ng-if correctly the element will be removed in the DOM if the expression evaluates to false. So the value will be true here on this.hideSensitiveData if I'm logged in as a customer. Still it removes the columns, which obviously confuses me since it should only remove it if it's false.

Thanks in advance!

EDIT: here is the dataservice funtion if it helps:

function getUserType() {
        return $http.get('/api/v2/user/getUserType/')
               .then(function (xhr) {
                   logger.info('userType loaded: ', xhr);
                   var userType = xhr.data;
                   return userType;
               })
               .catch(function (message) {
                   logger.error('XHR Failed for userType', message);
                   return $q.reject(message);
               });
    }

EDIT2: In response to the duplicate flag. I actually have var self = this; at the top of the controller... Is that why everyone got so confused? So I use self.hideSensitiveData instead of this.hideSensitiveData. Didn't know it mattered.

Philip B
  • 27
  • 1
  • 11
  • 4
    What is your angular version? Also what is `this`? – jsalonen Jul 02 '15 at 12:52
  • 1
    'this' seems to be suspicious to me – Anshul Jul 02 '15 at 12:55
  • Use $scope it only communicate between controllers and the view – Sudharsan S Jul 02 '15 at 12:55
  • Maybe they are using controllerAs this? Otherwise something seems off. – ajmajmajma Jul 02 '15 at 12:58
  • @jsalonen AngularJS v1.3.4. "This" is the window object or however you say it. In it are all the functions and objects for that instance. – Philip B Jul 02 '15 at 12:58
  • Same results with scope – Philip B Jul 02 '15 at 13:00
  • It would help if you could provide us with complete, working example that allows us to reproduce the problem. My best guess is that you should bind `this` correctly inside the callback function. – jsalonen Jul 02 '15 at 13:01
  • But I get exactly the same results even when I use $scope. So really that can't be the problem can it? – Philip B Jul 02 '15 at 13:05
  • Well I want it to be false if logged in as a customer and true if logged in as an admin. – Philip B Jul 02 '15 at 13:22
  • 1
    If you bind hideSensitiveData to $scope, ng-if="this.hideSensitiveData" should be ng-if="hideSensitiveData". If you are using ControllerAs syntax (e.g ng-controller="MyCtrl as Ctrl"), ng-if="this.hideSensitiveData" should be ng-if="Ctrl.hideSensitiveData". – timsmiths Jul 02 '15 at 14:50
  • @timsmiths that is what I did. I changed everything that was "this" to $scope. As you wrote in your first sentence. Still the same problem... – Philip B Jul 02 '15 at 15:06
  • Is 'sequence.pumpPressure.toFixed(1)' in the same scope of 'hideSensitiveData?' – timsmiths Jul 02 '15 at 15:39
  • @timsmiths I guess not. The data for 'sequence.pumpPressure.toFixed(1)' comes from the server, so it's under self(var self = this;) when debugging in chrome. I didn't write the code that gets that data. – Philip B Jul 03 '15 at 08:58

0 Answers0