【问题标题】:Cannot access value of ng-model variable of bind-unsafe-html无法访问 bind-unsafe-html 的 ng-model 变量的值
【发布时间】:2015-11-07 07:18:58
【问题描述】:

showdetailsOfInside()函数值bind-unsafe-htmlnameinagein的内部 /strong> 未定义,但是我已将文本内容填充到其中。我可以通过 jquery 获得价值,但有没有办法通过 angular js 来做到这一点。

<body ng-controller="AppController" class="container">
Name <input type="text" name="name" ng-model="name" /><br>
Age <input type="text" name="age"  ng-model="age"/><br>
<button type="button" class="btn btn-primary" ng-click="showdetailsOfInside()">showdetailsOfInside</button><br>
<div bind-unsafe-html="primaryData"></div>
<body>

bind-unsafe-html="primaryData"的内容

    <button type="button" class="btn btn-primary" ng-click="shownName()">inside Basic</button><br> 
    <button type="button" class="btn btn-primary"  ng-click="showAge()">inside Primary</button><br> 

Name inside <input type="text" name="namein" ng-model="namein" id="nameinside"/><br>
Age indise :: <input type="text" name="agein" ng-model="agein" id="ageinside"/><br>

bind-unsafe-html 指令代码

dynamicContentApp.directive('bindUnsafeHtml', ['$compile', function ($compile) {
    return function(scope, element, attrs) {
        scope.$watch(
          function(scope) {
            // watch the 'bindUnsafeHtml' expression for changes
            return scope.$eval(attrs.bindUnsafeHtml);
          },
          function(value) {
            // when the 'bindUnsafeHtml' expression changes
            // assign it into the current DOM
            element.html(value);

            // compile the new DOM and link it to the current
            // scope.
            // NOTE: we only compile .childNodes so that
            // we don't get into infinite loop compiling ourselves
            $compile(element.contents())(scope);
          }
      );
  };
}]);

【问题讨论】:

  • 指令应该返回对象而不是函数,检查指令文档

标签: javascript angularjs ngsanitize


【解决方案1】:

如果我是对的,那就是 ng-bind-unsafe-html="primaryData"

【讨论】:

  • 它是bind-unsafe-html。它的用户定义指令我在上面提到了指令代码。
【解决方案2】:
dynamicContentApp.directive('bindUnsafeHtml', ['$compile', function ($compile) {
    return {
        link: function (scope, element, attrs) {
            //code goes here 
        };
    }
}]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    相关资源
    最近更新 更多