0

Workflow: Passing JSON to angular scope and using ng-repeat in the View to display it.

Using following function to show dialog on click of elements of a particular class.

 $(function () {
        $("#dialog").dialog({
            autoOpen: false,
            show: {
                effect: "blind",
                duration: 1000
            },
            hide: {
                effect: "explode",
                duration: 1000
            }
        });

        $(".opener").on("click", function () {
            $("#dialog").dialog("open");
        });
    });

    $(function () {
        $("#imagePopup").dialog({
            autoOpen: false,
            show: {
                effect: "blind",
                duration: 1000
            },
            hide: {
                effect: "explode",
                duration: 1000
            }
        });

Want it to get triggered on click of a particular class.

  <tr ng-repeat="item in ItemList| filter:itemName ">
            @*<td>
                {{ item.CreatorID}}
            </td>*@
            <td >
                {{item.ItemName}}
            </td>
            <td >
                {{item.CreatorName}}
            </td>
            <td >
                {{item.Length}}
            </td>
            <td >
                {{item.Width}}
            </td>
            <td >
                {{item.Price}}
            </td>
            <td >
                {{item.ItemDescription}}
            </td>
            <td >
                {{item.Quantity}}
            </td>
            <td >
                <div class="opener" style="text-align:center">
                    <img ng-src="@Url.Content("{{item.ImagePath}}")" width="60" height="60" class="img-responsive img-circle" />
                </div>
             </td>
            <td >
                 <span class="glyphicon glyphicon-pencil"></span>
            </td>
            <td >
                <span class="glyphicon glyphicon-trash"></span>
            </td>   

        </tr>

    </table>
    <button class="opener">Opener</button>
    <button class="opener">Opener11</button>

    <div id="dialog" title="Item Image">
        <p>Sample text</p>            
    </div>

Here when I am clicking on Button Opener& Opener11 popup appears but not on the click of <div> which has the same class(inside ng-repeat).

Can anyone tell me if i'm missing anything?

kkakroo
  • 747
  • 1
  • 7
  • 17
  • Are the items being added dynamically (after the page has been rendered)? - in which case you need event delegation –  Mar 26 '17 at 11:03

1 Answers1

1

Use instead:

$(document).on("click", ".opener", function () {
       $("#dialog").dialog("open");
});

you are registering the event with the element which is not present on DOM while document ready event is triggered.

Reference Question: Event binding on dynamically created elements?

Community
  • 1
  • 1