ng-click doesn't work on dynamic DOM AngularJS?

前端 未结 1 1411
感动是毒
感动是毒 2020-12-19 02:10

I have a controller like this:

@VariantModalCtrl = ($scope) ->
  $scope.upload_variant_image = ->
    alert(\"test\")

When I try to c

相关标签:
1条回答
  • 2020-12-19 02:42

    I would warn you that you may not be fully embracing Angular philosophy if you're manipulating the DOM through Angular-external means. Adding links dynamically with AngularJS is as simple as anything else and will probably be much easier and more idiomatic than getting your external library to play nice. Here is a simple example based on your question:

    <ul>
      <li ng-repeat="item in items">
        <a ng-click="upload_variant_image()">{{item.name}}</a>
      </li>
    </ul>
    

    All you need to do is wire up the scope data appropriately and this will always "just work."

    That said, if you are manipulating the DOM, you can cause AngularJS bindings to occur (to, say, ng-click as you desire) by using $compile service. Do consider the above better option, though. :)

    0 讨论(0)
提交回复
热议问题