将服务注入指令返回undefined

时间:2017-07-13 15:55:30

标签: javascript angularjs

我正在将一个服务注入一个指令中,对于某些实例,这个服务返回undefined可以解释我做错了吗?

以下是代码的一部分内容。 https://plnkr.co/edit/H2x2z8ZW083NndFhiBvF?p=preview

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.players = ["A","B","C"];
});

app.factory('PlayerListS', [function() {
    var playerList = [];

    function getList() {
        return playerList;
    }
    function addToList(name) {
        playerList.push(name);
    }

    return {
        addToList :addToList,
        getList: getList
    }

}]);
app.directive("player",['PlayerListS', function (PlayerListS) {
return {
    restrict: 'E',
    scope: {
        person:'@person',
        add:'&add'
    },
    replace: false,
    templateUrl: "player.html",
    controller: function($scope, $element, $compile) {
          $scope.add = function(name) {
              PlayerListS.addToList(name);
              console.log(PlayListS.getList());
          }
    }
};
}]);

1 个答案:

答案 0 :(得分:2)

您的控制台中有一个拼写错误,因为代码会导致错误。以下列方式更改指令

{
    "declines": 143.0,
    "data": [{
        "symbol": "VISESHINFO",
        "xDt": "-",
        "caAct": "-",
        "iep": "0.10",
        "chn": "-0.05",
        "perChn": "-33.33",
        "pCls": "0.15",
        "trdQnty": "2,000",
        "iVal": "0.00",
        "mktCap": "-",
        "yHigh": "-",
        "yLow": "-",
        "sumVal": "5,629.00",
        "sumQnty": "16,66,005",
        "finQnty": "2,000",
        "sumfinQnty": "47,53,417"
    }, {
        "symbol": "NUTEK",
        "xDt": "-",
        "caAct": "-",
        "iep": "1.10",
        "chn": "0.15",
        "perChn": "15.79",
        "pCls": "0.95",
        "trdQnty": "26,312",
        "iVal": "0.29",
        "mktCap": "-",
        "yHigh": "-",
        "yLow": "-",
        "sumVal": "5,629.00",
        "sumQnty": "16,66,005",
        "finQnty": "26,312",
        "sumfinQnty": "47,53,417"
    }, {
        "symbol": "ORIENTALTL",
        "xDt": "-",
        "caAct": "-",
        "iep": "10.50",
        "chn": "-0.80",
        "perChn": "-7.08",
        "pCls": "11.30",
        "trdQnty": "250",
        "iVal": "0.03",
        "mktCap": "-",
        "yHigh": "-",
        "yLow": "-",
        "sumVal": "5,629.00",
        "sumQnty": "16,66,005",
        "finQnty": "250",
        "sumfinQnty": "47,53,417"
    }]
}

工作演示https://plnkr.co/edit/HhmOYyoZAhm6vvXp3puC?p=preview