Niz*_*zar 6 html javascript jquery angularjs
我创建了一个显示消息的指令,并且它可以呈现任何带有角度属性的html消息,它可能包括按钮,锚标签(带有ng-clicks属性)等等.
index.html的:
<ir-error-panel status="status"></ir-error-panel>
Run Code Online (Sandbox Code Playgroud)
irErrorPanel.tpl.html:
<div >
THE MESSAGE IS:<BR>
<div ng-bind-html="textHtmlSafe"></div>
</BR>
</div>
Run Code Online (Sandbox Code Playgroud)
irErrorPanel.js:
angular.module('ng-iridize-common.directives').directive('irErrorPanel', ['$sce', function($sce) {
return {
restrict: 'E',
templateUrl: 'irErrorPanel.tpl.html',
controller: ['$scope', '$log', function($scope, $log) {
var _this = this;
_this.applyMessageText = function applyMessageText() {
$scope.textHtmlSafe = $scope.status && $scope.status.text ? $sce.trustAsHtml($scope.status.text) : "";
};
_this.applyMessageText();
$scope.$watch("status.text", function(newValue, oldValue) {
if (newValue === oldValue) {
return;
}
if (!newValue) {
$scope.textHtmlSafe = "";
return;
}
_this.applyMessageText();
});
}],
link: function(scope, iElement, iAttrs, ctrl) {
//call service and check box status.
scope.$watch("status.text", function(value) {
$compile(iElement.contents())(scope);
})
}
}
}]);
Run Code Online (Sandbox Code Playgroud)
例如,当状态为:"click!"时,它呈现完全正常,但ng-click不会触发任何内容.
我最终创建了一个此处建议的新指令来动态编译 html。
.directive('compile', ['$compile', function ($compile) {
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
// watch the 'compile' expression for changes
return scope.$eval(attrs.compile);
},
function(value) {
// when the 'compile' 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);
}
);
};
}])
Run Code Online (Sandbox Code Playgroud)
并在 irErrorPanel.tpl.html 中替换
<div ng-bind-html="textHtmlSafe"></div>
Run Code Online (Sandbox Code Playgroud)
和:
<div compile="status.text"></div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
620 次 |
| 最近记录: |