【发布时间】:2015-11-07 07:18:58
【问题描述】:
showdetailsOfInside()函数值bind-unsafe-htmlnamein和agein的内部 /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