How to add ng-click handler dynamically

走远了吗. 提交于 2019-12-04 08:08:54

Issue is with this line of code:

$compile(generated)($scope);

Instead it should be:

$compile(generated.contents())($scope);

You can assign the function to a scope variable and depending on your business logic assign appropriate functions to your ng-click. In the below example $scope.addGeoFence() is added to the ng-click of "Add GeoFence" list-item

$scope.layout = [
      {name: 'Home', icon: 'home'},
      {name: 'Add Geofence', 
       icon: 'add_location', 
       click: $scope.addGeoFence}
];

$scope.addGeoFence = function() {
    console.log("calling addGeoFence()");
}

<md-list>
    <md-list-item ng-repeat="snav in layout">
        <md-button class="md-raised" ng-click="(snav.click)()" flex>
            <md-icon md-font-library="material-icons">{{snav.icon}}
            </md-icon>
            <span>{{snav.name}}</span>
        </md-button>
    </md-list-item>
</md-list>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!