javascript - How to insert HTML along with onclick function into view in angularjs -


i trying render html view , want insert function call inside html not getting idea how can this. here javascript:

        angular.foreach($scope.patients, function (patient) {          tablerow = tablerow + [              '<div data-ng-click="popup("+patient+")">',                  '<div class="name-container">+patient.name+</div>',               '</div>'].join('');     });     $scope.renderhtml =$sce.trustashtml(tablerow);      $scope.popup = function(patient) {     ...  }; 

html:

  <div data-ng-bind-html="renderhtml">              </div> 

is possible add patient object in popup() function using way ?

html: $eval helps evaluate different angular expressions

<div data-ng-bind-html="$eval(renderhtml)">              </div> 

js

it may you,

angular.foreach($scope.patients, function (patient) {          tablerow = tablerow + [              '<div data-ng-click="popup("'+patient+'")">',                  '<div class="name-container">'+patient.name+'</div>',               '</div>'].join('');     });     $scope.renderhtml =$sce.trustashtml(tablerow);      $scope.popup = function(patient) {     ...  }; 

you passing patient object in popup function, think may not work. if doesn't work try using json.stringify

refer following link more information how make ng-bind-html compile angularjs code

suggestion:

try use ng-repeat directive in place of angular.foreach. think can achieve requirement using ng-repeat, reference:

 <div ng-repeat="patient in patients">         <div data-ng-click="popup(patient)">            <div class="name-container">patient.name</div>            </div>         </div>     </div> 

modified:

try this

angular.foreach($scope.patients, function(value,key){          tablerow = tablerow + [              '<div data-ng-click="popup("'+patients[key]+'")">',                  '<div class="name-container">'+patients[key].name+'</div>',               '</div>'].join('');     });     $scope.renderhtml =$sce.trustashtml(tablerow);      $scope.popup = function(patient) {     ...  }; 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -