0

I have a number of <div> tags and I'm assigning them a function that allows me to display a kind of modal with details from the <div> element by changing it's css propriety 'display' to 'block' instead of 'none'. for some reason the modal doesn't display when I click on the <div> element. but when I assign the same function to a button it does.

$scope.CountryDetailStyle = {
  'display': 'none'
};

$scope.CountryDetail = function(idCount, nameCount, descCount) {
  $scope.detailId = idCount;
  $scope.detailName = nameCount;
  $scope.detailDesc = descCount;
  $scope.CountryDetailStyle = {
    'display': 'block'
  };
  // alert('idCount : '+idCount+' nameCount : '+nameCount+' descCount : '+descCount);
}
<div id="pageContent" ng-controller="myAdminController">
  <div class="countries" ng-repeat="country in countries" title="{{country.descCount}}" ng-click="CountryDetail(country.idCount, country.nameCount,
        country.descCount)">
    <label> {{country.nameCount}} </label><br/>
    <img ng-src="uploads/{{country.image}}" alt="Image Not Found"><br>
  </div>
</div>


<div>
  <button ng-click="CountryDetail(country.idCount, country.nameCount,
        country.descCount)" style="display:block; float:right; clear:both;">Display Country Detail</button>
</div>


<div class="countryDetails" ng-style=CountryDetailStyle>
  <!--this is the modal i want to display -->
  <div class="content">
    <div class="boxHeader">
      <label>{{detailName}}</label>
    </div>
    <div class="boxContent">
      <form>
        <p>{{detailDesc}}</p>
        <a href="#" class="btn" style="text-decoration:none;">Read More</a>
      </form>
    </div>
  </div>
</div>

can someone tell me what's the problem? thanks.

4
  • 1
    Could you post a link to working code? Here for example:jsfiddle.net Commented Oct 17, 2017 at 9:39
  • 1
    Your div class="countryDetails" is outside the div containing the ng-controller directive. Commented Oct 17, 2017 at 9:44
  • I'm using a php file to retrieve my data from server. I don't know how to upload that with the other files for it to work properly! here is the link link Commented Oct 17, 2017 at 9:47
  • @SergeK. in my script file I have to controllers and I'm using both in my html file. I'm adding the ng-controller to the <body> tag Commented Oct 17, 2017 at 9:52

3 Answers 3

1

you should not display/hide elements based on some display: none condition. thats what ng-show (and ng-if) is for. Use a $scope variable like $scope.isModalVisible and set that to true or false.

Then you can use on your element ng-show="isModalVisible"

Sign up to request clarification or add additional context in comments.

2 Comments

tried that too, still doesn't work with the <div> element. but it works fine with a <button>
@I_Nesrine please update your example and add missing parts. it's impossible to debug if half of it is missing. A jsfiddle or sth else were you can run the code would be nice
0

var app=angular.module("myApp",[]);
app.controller("myAdminController",["$scope",function($scope){
$scope.CountryDetailStyle = {'display': 'none'};
$scope.countries=[
{
idCount:"1",
nameCount:1,
descCount:1

},{
idCount:"2",
nameCount:2,
descCount:2

},{
idCount:"3",
nameCount:3,
descCount:3

},{
idCount:"4",
nameCount:4,
descCount:4

}
]
                                
$scope.CountryDetail = function(idCount, nameCount, descCount){
    $scope.detailId = idCount;
    $scope.detailName = nameCount;
    $scope.detailDesc = descCount;
    $scope.CountryDetailStyle = {'display': 'block'};

}
}]);
<html lang="en">

<head></head>
<body ng-controller="myAdminController" ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="pageContent">
        <div class="countries" ng-repeat="country in countries" title="{{country.descCount}}" ng-click="CountryDetail(country.idCount, country.nameCount,
        country.descCount)">
            <label> {{country.nameCount}} </label><br/>
   
        <button ng-click="CountryDetail(country.idCount, country.nameCount,
        country.descCount)" style="display:block; float:right; clear:both;">Display Country Detail</button>
    </div>

     <br><br>
     <div class="countryDetails" ng-style=CountryDetailStyle>
        <div class="content">
            <div class="boxHeader">
                <label>{{detailName}}</label>
            </div>
            <div class="boxContent">
                <form>
                    <p>{{detailDesc}}</p>
                    <a href="#" class="btn" style="text-decoration:none;" >Read More</a>
                </form>
            </div>
        </div>
    </div>
    </body>
    </html>

4 Comments

you placed the <button> inside the <div> element?
country object will be available only in repeating container so that's why i placed button in div tag. Outside the container country object will be null
my objective is to display the modal when I click on the <div> element not the button. I only used the button to test if my function works.
so you can add ng-click for div
0

So I found a solution, still don't know why it works but it does. I moved the </div> of the div with the id="pageContent" to just before the body closing tag. That fixed everything. Thank you guys for the help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.