簡體   English   中英

如何在使用編譯而不是鏈接的指令中的角度中使用矩量庫

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

我的數據庫中有一個名為Actions的模型,其中包含連接文檔的樹(父級到子級),它們以有角度的指令遞歸地編譯,以便嵌套在其父級中。

我有以下代碼:

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>

您可以看到它再次在底部再次調用自身。 我也在使用RecursionHelper,所以無限循環不是問題。

除了使用setInterval ,您還需要使用Angular包裝器服務$interval

$interval服務通過內部調用$ scope。$ apply來執行視圖循環來同步視圖和模型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM