添加后加载时,ng-click 不会触发 [英] ng-click doesn't fire when added post load

查看:27
本文介绍了添加后加载时,ng-click 不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个变量替换使用 ngClick 使其可点击.

I'm trying to do a variable replacement while also making it clickable with ngClick.

我做了一个 plunker 演示(点击按钮并观察输入框保持不变)

I made a plunker demo (click the button and observe that the input box stays unchanged)

标记:

<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'.

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.

推荐答案

同时官方有解决方案:

<p translate="varWithDirective" translate-compile></p>

无需编写自定义指令即可进行编译.

would do the compilation without the need of writing a custom directive.

欲了解更多信息:https://github.com/angular-translate/angular-translate/issues/184

这篇关于添加后加载时,ng-click 不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆