Two controllers different HTML pages using same factory in Angularjs, but array is not updating on one?

痞子三分冷 提交于 2019-12-06 17:04:22

问题


I am stuck with Angular. I am trying to update an array on two different HTML pages, but this does not work. I tested this with giving the array an initial value and both pages display the values properly. Only when updating nothing happens on one page. I have tried these two things, but no luck and no errors in the console:

  .controller("ctrl1", function ($scope, Sender) {
        $scope.Sender = Sender;
        $scope.paired = Sender.paired;
        })

    .controller("ctrl2", function ($scope, Sender) {
        $scope.Sender = Sender;
        $scope.paired = Sender.paired;
        })

    .factory("Sender", function () {
        return {
            paired: [],
            fn: function (val) {
            this.paired.push(val);
              }
        }
    })

And this:

  .controller("ctrl1", function ($scope, Sender) {
        $scope.Sender = Sender;
        $scope.paired = Sender.paired;
        })

    .controller("ctrl2", function ($scope, Sender) {
        $scope.Sender = Sender;
        $scope.paired = Sender.paired;
        $scope.$watch('Sender.paired', function (newVal, oldVal, scope) {
        if(newVal) { 
        scope.paired = newVal;
    }
  });
        })

    .factory("Sender", function () {
        return {
            paired: [],
            fn: function (val) {
            this.paired.push(val);
              }
            }
        })

Thanks in advance for any suggestions.

来源:https://stackoverflow.com/questions/28182967/two-controllers-different-html-pages-using-same-factory-in-angularjs-but-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!