Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass value into ng-if after ng-click?

Purpose

I am trying to get admin and customer show in different stages, admin can post the data after clicking the toggleShowDiv(), which allows customer to see the data.

Question

How to pass !isAdmin() into ng-if? Currently, I am only getting isAdmin as default.

Is able to post it into table TD by TD (row by row)? not sure, I am writing the correct code here.

My thought

Can I use ng-if to each single TD = isAdmin() or !isAdmin, and control by a click function?

$scope.showDiv = isAdmin();

$scope.toggleShowDiv = function (auction) {
  var title = 'text.......';
  var text = 'are you sure?';

  ConfirmModal(title, text, function () {
    $scope.showDiv = !isAdmin() ;
  });
};

HTML

<div ng-if="showDiv">
  <tbody class="auction-group" ng-repeat="a in foos">
    <td ng-if="isAdmin()">
      <input type="checkbox" ng-click="toggleShowDiv()" />
    </td>
</div>

Update

isAdmin() is just a function that passed from the backend.

function isAdmin() {
  return !!($aScope.currentUser && $aScope.currentUser.isAdministrator);
}

Please note: the question is not about the isAdmin() function, it works fine. What I want to do is to use a click function to show and hide the table row.

like image 474
Fenici Avatar asked Sep 11 '17 13:09

Fenici


Video Answer


3 Answers

Have a look at this. Here you have 2 users online at the same time, dude1 (admin) and dude2 (non admin). You can toggle the display from the admin side for the non admin side by having a call to the back end that continuously checks if the display is valid or not. For putting a toggle on the table rows you need to just add the ng-if to the <tr> elements.

var app = angular.module('app', []);

app.controller("controller", function($scope) {
    $scope.dude1 = {admin: true, name: [{name: 'A+', country:'India', publish: true}, {name: 'A', country:'Unknown', publish: true}]};
    $scope.dude2 = {admin: false, name: [{name: 'A+', country:'India', publish: true}, {name: 'A', country:'Unknown', publish: true}]};
    
    $scope.toggler = (index) => {
        $scope.dude1.name[index].publish = !$scope.dude1.name[index].publish;
    };
    
    $scope.names = (dude) => {
        return dude.name;
    };
    
    setInterval(() => {
        /**
         * Any backed function to get and repopulate the data.
         * Update the value of publish from the server. I'm just using
         * the other guys data. But you should fetch it from the server.
         */
        $scope.dude2 = valfromServer();
        // console.log($scope.dude2, $scope.dude1);
    }, 2000);
    
    var valfromServer = () => {
        return {
            admin: false,
            name: $scope.dude1.name
        };
    };
    
    $scope.publish = (dude, index) => {
        return dude.admin || dude.name[index].publish;
    };
    
    $scope.isAdmin = (dude) => {
        return dude.admin;
    };
});
<!DOCTYPE html>
<html>

<head>
    <script data-require="[email protected]" data-semver="1.6.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script>
</head>

<body ng-app="app" ng-controller="controller">
    <span>Admin Panel</span>
    <div>
        <table style="width:40%">
            <tr ng-repeat="x in names(dude1)" ng-if="publish(dude1, $index)">
                <td>{{ x.name }}</td>
                <td>{{ x.country }}</td>
                <td>{{ $index }}</td>
                <td><button ng-click="toggler($index)" ng-if="isAdmin(dude1)">Publish</button></td>
            </tr>
        </table>
    </div>
    
    <hr>
    <span>Non admin panel</span>
    <div>
        <table style="width:40%">
            <tr ng-repeat="x in names(dude2)" ng-if="publish(dude2, $index)">
                <td>{{ x.name }}</td>
                <td>{{ x.country }}</td>
                <td>{{ $index }}</td>
                <td><button ng-click="toggler($index)" ng-if="isAdmin(dude2)">Publish</button></td>
            </tr>
        </table>
    </div>
</body>

</html>
like image 134
TheChetan Avatar answered Oct 11 '22 14:10

TheChetan


<div ng-if="showDiv == true || isAdmin == true">
  <tbody class="auction-group" ng-repeat="a in foos">
    <td ng-if="isAdmin == true">
      <input type="checkbox" ng-click="toggleShowDiv()" />
    </td>
</div>

JS code Let say first any one who enters will be customer

$scope.showDiv = false;
$scope.isAdmin = false;

now when response comes form backend check the response and change the value of $scope.isAdmin accordingly.

if(response == admin){
  $scope.isAdmin= true;
}else{
  $scope.isAdmin = false;
}

now in onclick checkbox function

$scope.toggleShowDiv = function (auction) {
   var title = 'text.......';
   var text = 'are you sure?';

   ConfirmModal(title, text, function () {
     if($scope.showDiv == false){
        $scope.showDiv = true;
     }else{
        $scope.showDiv = false;
     }

   });
};
like image 20
Anil Shrestha Avatar answered Oct 11 '22 12:10

Anil Shrestha


Well I think that you should use some var that change according if the user click like $scope.showTable = true /false. But not complety sure about your real need.

like image 23
cesarlarsson Avatar answered Oct 11 '22 12:10

cesarlarsson