I'm trying to do a variable replacement while also making it clickable with ngClick.
I made a plunker demo (click the button and observe that the input box stays unchanged)
Markup:
<body ng-controller="Ctrl">
<input type="text" id="input" name="input" ng-model="myValue" test>
<p translate="VARIABLE_REPLACEMENT" translate-values="{{ 'translationData' }}"></p>
<p ng-bind-html="alink"></p>
</body>
Angular stuff:
var translations = {
VARIABLE_REPLACEMENT: 'Hi, {{name}}'
};
var app = angular.module('myApp', ['pascalprecht.translate', 'ngSanitize']);
app.config(['$translateProvider', function ($translateProvider) {
// add translation table
$translateProvider.translations(translations);
}]);
app.controller('Ctrl', ['$scope', '$sce', function ($scope, $sce) {
$scope.translationData = { name: 'foo'};
$scope.setValue = function(value) {
$scope.myValue = value;
};
}]);
app.directive('test', ['$translate', '$sce', function ($translate, $sce) {
return {
require: 'ngModel',
scope: false,
link: function (scope, element, attrs, ctrl) {
scope.$watch(attrs.ngModel, function (value) {
var a = $translate('VARIABLE_REPLACEMENT', {
name: '<button type="button" ng-click="setValue('foobar')">click me</button>'
});
scope.alink = $sce.trustAsHtml(a);
});
}
};
}]);
The question is: Why doesn't ng-click="setValue('foobar')"
fire when the button is clicked. It's supposed to set the value 'foobar' in the input field.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…