简体   繁体   English

服务中的数组上的$ watch在指令中不起作用

[英]$watch on array in service not working in directive

I am trying to watch when an array in a service is updated. 我正在尝试观察服务中的数组何时更新。 I update this array in the service using a function in a directive based controller. 我使用基于指令的控制器中的函数在服务中更新此数组。 Now for some reason the watch function does not get called in the link function of the second directive. 现在由于某种原因,第二个指令的链接函数中没有调用watch函数。 Why is watch not being called in the second directive. 为什么第二个指令中没有调用watch。 I am trying to update the scope of a variable in the second directive so that it updates when the first directive function updates the service. 我正在尝试在第二个指令中更新变量的范围,以便在第一个指令函数更新服务时更新。

The Service 服务

var productServices = angular.module('productServices', ['ngResource']);
productServices.factory('PlayerListS', [function() {
    var playerList = [];

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

    return {
        addToList :addToList,
        getList: getList
    }
}]);

The Directives 指令

'use strict';

bands.directive("player",['PlayerListS', function (PlayerlistS) {
return {
    restrict: 'E',
    scope: {
        person:'@person',
        add:'&add'
    },
    replace: false,
    templateUrl: "partials/player.html",
    controller: function($scope, $element, $compile) {
        $scope.playerList = ["A", "B"];
        $scope.add = function(name) {
            PlayerlistS.addToList(name);
            PlayerlistS.getList();

        }

    },
    link: function(scope, el, attrs) {

    }

};
}]);
bands.directive("playerList", ['PlayerListS', function (PlayerlistS) {
return {
    restrict: 'E',
    replace: false,
    template: "<p>Test</p>",
     controller: function($scope, $element, $compile) {
    },
    link: function($scope, $el,$attrs) {
        console.log('added');
        var x = PlayerlistS.getList()

/*THIS IS WHERE THE WATCH IS HAPPENING*/
        $scope.$watch('x', function (newVal, oldVal) {
                console.log("CHANGED");
        }, true);
    }

};
}]);

The Controller 控制器

var bands = angular.module('bands', []);
bands.controller('ViewHousesCtrl',  ['$scope', '$element', '$routeParams', '$q',
function ViewHousesCtrl($scope, $element, $routeParams, $q) {

    $scope.playerLis = ["A","B","C"];

}]);

HTML 的HTML

   <player ng-show="true" person="RandomName" add="add()"></player>
   <player-list ng-show="true" ng-repeat="a in playerLis"></player-list>

What your watcher is really doing, is trying to watch a variable called x on the directive scope. 您的观察者真正在做的是尝试在指令范围内观察一个名为x的变量。 But your variable x is just a regular local variable, so your watcher doesn't trigger. 但是您的变量x只是常规局部变量,因此您的观察者不会触发。 So what your watcher basically translates to is this: 所以您的观察者基本上可以转换为:

$scope.$watch(function(scope){
    return scope['x'];
}, function (newVal, oldVal) {
    console.log("CHANGED");
}, true);

You can probably see why it doesn't trigger. 您可能会明白为什么它不触发。 There is no variable $scope.x . 没有变量$scope.x Instead you should try watching the service directly, by specifying the watch function. 相反,您应该尝试通过指定监视功能来直接监视服务。 Like this: 像这样:

$scope.$watch(function(){
    return PlayerlistS.getList();
}, function (newVal, oldVal) {
    console.log("CHANGED");
}, true);

您的HTML中有一个拼写错误,应该是:

<player-list ng-show="true" ng-repeat="a in playerList"></player-list>

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

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