简体   繁体   English

如何在使用编译而不是链接的指令中的角度中使用矩量库

[英]How to use the moment library in angular in a directive that uses compile instead of link

I have a tree of connected documents (parent to child) in my database from a single model called Actions, they're recursively compiled in an angular directive so that they're nested inside their parents. 我的数据库中有一个名为Actions的模型,其中包含连接文档的树(父级到子级),它们以有角度的指令递归地编译,以便嵌套在其父级中。

I have the following code: 我有以下代码:

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);
        });
      }
    };
  });

This only compiles once on load and changing anything in the scope after does nothing. 这只会在加载时编译一次,而在什么都不做之后更改范围中的任何内容。 I want the setInterval function to change a variable scope.fromNow to be updated every second and update the view (the HTML references this with a simple {{fromNow}}) 我希望setInterval函数更改变量scope.fromNow每秒更新一次并更新视图(HTML使用简单的{{fromNow}}对此进行引用)

I believe I'll have to re-compile the directive somehow but doing something like: 我相信我将必须以某种方式重新编译指令,但必须执行以下操作:

$compile(element.contents())(scope)

within the setInterval function doesn't work. 在setInterval函数中不起作用。

My directive's HTML looks like this: 我的指令的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>

You can see that it calls itself again right at the bottom. 您可以看到它再次在底部再次调用自身。 I am also using RecursionHelper so infinite loop isn't an issue. 我也在使用RecursionHelper,所以无限循环不是问题。

Instead of using setInterval , you need to use the Angular wrapper service $interval . 除了使用setInterval ,您还需要使用Angular包装器服务$interval

$interval service synchronizes the view and model by internally calling $scope.$apply which executes a digest cycle. $interval服务通过内部调用$ scope。$ apply来执行视图循环来同步视图和模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM