Angular 1的自定义指令,自定义过滤器的操作

angular 自定义指令,实现ng-repeat渲染完成之后执行指定的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// js代码
// 定义指令
var repeatFinish = angular.module("repeatFinish",[]);
repeatFinish.directive('repeatFinish', function () {
return {
link: function (scope, element, attr) {
if (scope.$last === true) {
scope.$eval(attr.repeatFinish);
}
}
}
});

// 引入指令
var app = angular.module("app", ['repeatFinish']);
app.controller("material_edit_ctrl", ["$scope", function ($scope) {
$scope.init_um_editor = function () {
console.log("repeat-finish");
}
}]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 页面代码 -->
<body ng-app="app" >
<ul ng-controller="ctrl" class="news-list">
<li repeat-finish="init_um_editor()" class="new" ng-repeat="new in news" ng-init="index = $index">
<div class="control-group">
<label class="control-label">标题
<span class="form-required" style="color:red;">*</span>
</label>
<div class="controls">
<textarea style="resize:none;width: 80%" placeholder="请在这里输入标题" name="title" cols="100" rows="3" index="{{index}}">{{new.title}}</textarea>
</div>
</div>
</li>
</ul>
</body>

angular自定义过滤器实现将str中的转义字符还原成html字符

1
2
3
4
5
6
7
// js代码
var app = angular.module("app", ['repeatFinish']);
app.filter('trust2Html', ['$sce',function($sce) {
return function(val) {
return $sce.trustAsHtml(val);
};
}]);
1
2
3
4
5
6
7
8
9
10
<!-- html代码 -->
<ul ng-controller="ctrl" class="news-list">
<li repeat-finish="init_um_editor()" class="new" ng-repeat="new in news" ng-init="index = $index">
<div class="control-group">
<div class="controls">
{{new.content | trust2Html}}
</div>
</div>
</li>
</ul>