I have a "favorite/unfavorite" button which toggles it function once user click it.
<button id="favoriteButton" class="btn btn-default btn-sm" type="button" ng-click="@Model.GetFavoriteClick()">
<span aria-hidden="true" class="@Model.GetFavoriteClass()"></span> @Model.GetFavoriteText()
</button>
in my controller I am handling click this way;
$scope.Favorite = function (providerContentId) {
var request = $http({
method: "post",
url: "/api/Favorite",
params: {
// query string params
},
data: {
providerContentId: providerContentId
}
});
request.then(function (response) {
// here is it replacing but new ng-click is not working
var element = angular.element(document.querySelector('#favoriteButton'));
element.replaceWith(response.data);
}, function (response) {
alert(response.data);
});
};
while I was searching, I see I would use $compile service to do that, but I could not make it work.
var content = $(response.data);
angular.element(document).injector().invoke(function ($compile) {
var scope = angular.element(content).scope();
$compile(content)(scope);
How can I do that?