无法访问bind-unsafe-html的ng-model变量的值

时间:2015-08-14 05:57:41

标签: javascript angularjs ngsanitize

bind-unsafe-html <内的 showdetailsOfInside() ng-model变量 namein agein 的功能值/ strong>未定义但是我已将文本内容填入其中。我可以通过jquery获取值但是有没有办法通过角度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 =&#34; primaryData&#34;

的内容
    <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);
          }
      );
  };
}]);

2 个答案:

答案 0 :(得分:0)

如果我没错,那就是ng-bind-unsafe-html =&#34; primaryData&#34;

答案 1 :(得分:0)

dynamicContentApp.directive('bindUnsafeHtml', ['$compile', function ($compile) {
    return {
        link: function (scope, element, attrs) {
            //code goes here 
        };
    }
}]);