New to AngularJS - I am trying to create a function that finds "tokens" of specific text, replaces them with an ng-click
directive bound to a function on the controller. For example, from my database I have lots of text-fields with something like this:
<p>Your [labor] cost is included in price</p>
Which I would like to end up like this:
<p>Your <a href="#" ng-click="showModal('labor')">labor</a> cost is included in price.</p>
I have no problem replacing the [labor]
token with a filter, but I am a bit lost on how to incorporate $compile
to bind the ng-click
to $scope.showModal()
on my controller.
Any help would be appreciated. Thank you.
My existing filter:
myApp.filter('parseHtml', function ($sce) {
return function (text) {
var link = '<a href="" ng-click="getModal(\'labor\')">Labor</a>';
var replaced = text.replace(/\[labor\]/g, link);
return $sce.trustAsHtml(replaced);
};
});
In the html
<span ng-bind-html="package.price | parseHtml"></span>
The controller
myApp.controller('MainController',
function MainController($scope, $http) {
$scope.getpackage = function (slug) {
var onpackageComplete = function (response) {
$scope.package = response.data;
};
$http.get('/packages/api/' + slug + '/detail')
.then(onpackageComplete);
};
$scope.getModal = function (name) {
$scope.modalVisible = true;
if (name === 'labor') {
$scope.modalBody = '/path/to/html/snippet/ModalLabor.html';
} else if (name === '') {
$scope.modalBody = '';
}
};
}
);