指令范围被覆盖

时间:2019-04-15 07:26:57

标签: angularjs angularjs-directive angularjs-scope

我有此指令,每当执行Exception in thread "main" java.lang.NoSuchMethodError: org.json.JSONObject.entrySet()Ljava/util/Set; at org.json.XML.toString(XML.java:502) at org.json.XML.toString(XML.java:471) at com.testing.main(testing.java:13) 时,出于某种原因,当我使用ng-repeat时,它总是返回console.log中的最后一个数据。

ng-repeat

这是ng-repeat的代码。(使用json管道时的数据正确。)

(function () {
    'use strict';

    angular
        .module('app.schoolAdmin')
        .directive('barGraph', barGraph);

    barGraph.$inject = ['$timeout'];

    function barGraph ($timeout) {
        var scope;
        var element;

        return {
            link: link,
            restrict: 'EA',
            scope: {
                data: '=',
                onClick: '&',
                methods: '='
            }
        };

        function init () {
            console.log("DATA", scope.data);
        }

        function link (_scope, _element) {
            scope = _scope;
            element = _element;

            $timeout(function () {
                init();

            }, 0);

        }
    }
})();

1 个答案:

答案 0 :(得分:2)

$timeout(function () {
   init();
}, 0);

您上面的timeout引起了问题。

将其删除以解决此问题。

如果要保留$timeout,可以将_scope传递给init方法,如下所示:

  function init (data) {
    console.log("DATA", scope.data);
    console.log("REAL DATA", data);
  }

  function link (_scope, _element, attrs) {
    scope = _scope;
    element = _element;
    $timeout(function () {
      init(_scope.data);
    }, 0);

  }

这是一个示例jsfiddle

相关问题