jcubic jcubic - 2 months ago 7
AngularJS Question

Angular indicate that camel case property is always false

I have this code and angular indicates that

dahsboard.isCurrentUserOwner
is always false even if it is true.



angular.module('app', []).controller('main', function($scope) {
$scope.dashboards = [{"id":23,"name":"AFM Dashboard","description":"Dashboard displaying the size of the queue for each of the AFM replicated filesets. If the queue_length is > 0 and is a flat line there may be some issue with the replication of the data.","isCurrentUserOwner":true},{"id":24,"name":"GPFS","description":"GPFS state summary dashboard.","isCurrentUserOwner":false},{"id":25,"name":"test1231","description":"testtesttesttest 234567890-1","isCurrentUserOwner":false},{"id":28,"name":"test","description":"test","isCurrentUserOwner":true}];
$scope.sort = 'asc';
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.js"></script>
<div ng-app="app" ng-controller="main">
<div ng-repeat="dashboard in dashboards">
<div>{{dashboard|json}}</div>
<div>{{dahsboard.isCurrentUserOwner ? 'true' : 'false'}}</div>
<div ng-if="dahsboard.isCurrentUserOwner">
show
</div>
<div ng-if="!dahsboard.isCurrentUserOwner">
not show
</div>
</div>
</div>





I've also tried to use
dahsboard["is-current-user-owner"]
but it doesn't work, it's still false even if it's true.

Answer

You have a misspelled problem change, dahsboard to dashboard.

But IMHO try to use controller as syntax it's better to access the model that you have inside your controller and in the future will be better to work following the guides that will help you to don't have big problems with scopes.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.js"></script>
<div ng-app="app" ng-controller="main as mainCtrl">
   <div ng-repeat="dashboard in mainCtrl.dashboards">
       <div>{{dashboard|json}}</div>
       <div>{{dashboard.isCurrentUserOwner ? 'true' : 'false'}}</div>
       <div ng-if="dashboard.isCurrentUserOwner">
         show
       </div>
    <div ng-if="!dashboard.isCurrentUserOwner">
        not show
    </div>
  </div>
</div>

This should fix the problem you have accessing your model. This is the recommendation using Angular 1.

https://toddmotto.com/digging-into-angulars-controller-as-syntax/

John Papa GuideLine https://github.com/johnpapa/angular-styleguide


EDIT

(Needed 6 characters) Correct dahsboard on all lines.

Comments