用户存在但仅在执行自定义指令后加载

时间:2015-07-17 14:10:37

标签: javascript angularjs

从帖子Second for loop will not trigger in my $scope function继续

我的用户被加载到$ rootScope但是当我将它传递给我的函数scope.linkTaskToUser并将其放在for循环中以获得它返回0的长度。所以现在我需要知道如何激活我的指令使用函数scope.linkTaskToUser。

contentEditor.directive('custom', function($document, $rootScope) {
        return {
            restrict: 'E',
            scope: { email: '=' },
            link: function(scope, element, attrs) {  
                var mail = scope.email;

                $rootScope.users.push({
                    "email": mail,
                    "positionTop": element[0].offsetTop
                });

                //console.log("User email is: " + mail);
                //console.log("Y positon of a user circle " + element[0].offsetTop);
            }
        }
    });

var getTaskFunction = function(successFn, errorFn)
        {
              // original implementation of $scope.get_user but with success & error used which as passed in as parameter.
              $http.get('/api/task/all') // call the server

              .success(function(data) { // upon success
                  successFn(data); // call the function passed into the "getUserFunction" with the data from the server
                  //console.log("Retrieved task data from server");
                  //console.log("logging data: " , data);

                  $scope.updateGridDataAwesome();
                  //console.log("Barts test 1 ->", $scope.taskInfo);
              })
              .error(errorFn || function() {
                  console.log("Error in retrieving data from server");
              }) // process failure
          };

          this.reloadTaskList = function() {
          getTaskFunction(
              /* success function */
              function(data) {
                  //debugger;
                  $scope.taskInfo = data;
                  $scope.linkTaskToUser(data);
              },
              /* error function */
              function()
              {
                console.log("Server load failed");
              }
            )
        };

        // load list on startup
        this.reloadTaskList();

$scope.linkTaskToUser = function() {
        console.log("Logging root scope ", $rootScope);

        for (var i = 0; i < $scope.taskInfo.length; i++) { //Doorlopen van alle taken

            //console.log('Logging scope taskInfo -->' , $scope.taskInfo);
console.log('fasfsda -->', $rootScope.users.length);
            for (var j = 0; j < $rootScope.users.length; j++) { //Doorlopen van alle users
                console.log('fasfsda -->');
                if ($rootScope.users[j].email == $scope.taskInfo[i].email) {
                    console.log('fasfsda1111'); //Vergelijken van emails tussen taken en users
                    $scope.taskInfo[i].top = $rootScope.users[j].offsetTop; //Top waarde van de taak gelijkstellen aan de offset van de user
                }
            }
        }
    }

0 个答案:

没有答案