Javascript 如何在使用compile而不是link的指令中使用angular中的力矩库
我的数据库中有一个连接文档树(父到子),它来自一个名为Actions的模型,它们在angular指令中递归编译,以便嵌套在父对象中 我有以下代码:Javascript 如何在使用compile而不是link的指令中使用angular中的力矩库,javascript,angularjs,recursion,angularjs-directive,momentjs,Javascript,Angularjs,Recursion,Angularjs Directive,Momentjs,我的数据库中有一个连接文档树(父到子),它来自一个名为Actions的模型,它们在angular指令中递归编译,以便嵌套在父对象中 我有以下代码: angular.module('crmDashboardApp') .directive('actionDirective', function ($http, $compile, RecursionHelper) { return { scope: { actionId: '=', // html:actio
angular.module('crmDashboardApp')
.directive('actionDirective', function ($http, $compile, RecursionHelper) {
return {
scope: {
actionId: '=', // html:action-node, js:actionNode
actionList: '='
},
templateUrl: 'app/main/directive/action/action.directive.html',
replace: true,
restrict: 'E',
compile: function (element) {
return RecursionHelper.compile(element, function(scope, iElement, iAttrs, controller, transcludeFn){
scope.deleteAction = function (_action) {
var id = _action._id;
$http.delete('/api/actions', {
data: {'id':id},
headers: {"Content-Type": "application/json;charset=utf-8"} // we need to do this if we want to send params, otherwise we need to do traditional REST in URL
});
};
// Find for already called action list
scope.findAction = function (_id, _list) {
scope.actionNode = _.findWhere(_list, {_id:_id})
};
scope.findAction(scope.actionId, scope.actionList);
function calculateTimeSince(){
scope.fromNow = moment(scope.actionNode.content).fromNow(true);
}
setInterval(calculateTimeSince, 1000);
scope.fromNow = moment(scope.actionNode.content).fromNow(true);
});
}
};
});
这只在加载时编译一次,在加载后更改作用域中的任何内容都不会执行任何操作。我希望setInterval函数更改变量scope.fromNow,使其每秒更新一次,并更新视图(HTML使用一个简单的{{fromNow}}引用它)
我相信我必须以某种方式重新编译该指令,但要执行以下操作:
$compile(element.contents())(scope)
在setInterval内,函数不起作用
我的指令的HTML如下所示:
<div class="action-node">
<header>{{ actionNode.name }}</header>
<div class="row">
<h3 class="col-md-12">{{ actionNode.title }}</h2>
<h5 class="col-md-12">{{ actionNode.description }}</h5>
</div>
<div class="row">
<div class="col-md-3">Time Since: {{fromNow}}</div>
<div class="col-md-3">Content: {{ actionNode.content}}</div>
<div class="col-md-3">Duration Type:{{ actionNode.duration_type }}</div>
<div class="col-md-3">Type: {{ actionNode.type }}</div>
</div>
<div class="row">
<div class="col-md-4">
{{actionNode.children.length > 0 ? actionNode.children : "No children" }}
</div>
<form class="pull-right" ng-submit="deleteAction(actionNode)">
<input class="btn btn-primary" type="submit" value="Delete">
</form>
</div>
<div class="action-wrapper" ng-repeat="child in actionNode.children" ng-if="actionNode.children.length > 0">
<!-- <div class="row" ng-repeat="child in actionNode.children" ng-if="actionNode.children.length > 0" ng-style="{'margin-left': ({{actionNode.nest_level}}+1)*30+'px'}"> -->
<action-directive action-ID="child" action-list="actionList" />
</div>
</div>
{{actionNode.name}
{{actionNode.title}}
{{actionNode.description}}
自:{{fromNow}}起的时间
内容:{{actionNode.Content}
持续时间类型:{{actionNode.Duration\u Type}
类型:{{actionNode.Type}
{{actionNode.children.length>0?actionNode.children:“无子项”}
你可以看到它在底部再次调用自己。我也在使用RecursionHelper,所以无限循环不是问题。您需要使用Angular wrapper服务
$interval
,而不是使用setInterval
$interval
服务通过内部调用$scope.$apply来同步视图和模型,后者执行摘要循环。改用$interval服务,看看它是否工作。它起作用了。谢谢你能把你的评论复制粘贴到答案上吗?