我正在尝试进行变量替换,同时还可以使用ngClick使其可点击。
我做了一个plunker demo(点击按钮,观察输入框保持不变)
标记:
<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>
角的东西:
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);
});
}
};
}]);
ng-click="setValue('foobar')"
问题是:为什么单击按钮时不触发。它应该在输入字段中设置值“foobar”。