$ scope。$ watch in angularjs来更新服务的价值

时间:2015-12-12 05:26:11

标签: angularjs controller watch

我有定期更新的服务,我正在尝试将其更新值放在前端。

我的服务看起来像 -

akiRepair.factory("webService", ['$q', '$rootScope', function($q, $rootScope){
  ...
  this.repair = {};
  this.updates = {};
  this.parts = {};
  var t = this;
  ...
  var handle_subscribe = function(res){
      if(res.result == false){
        console.log("Unable to subscribe");
      }else{
        //update top widget

        t.repair.head = res.order.equipment.description;
        t.repair.subDate = res.order.submitted;
        t.repair.des = res.order.description.replace(/(?:\r\n|\r|\n)/g, '<br />');

        //update left widget
        t.updates = res.order.labour;

        //update right widget
        t.parts = res.order.charges;

      }
    };

虽然控制器看起来像 -

akiRepair.controller("restCtrl", ['$scope', 'webService', function($scope, webService){
  $scope.repair = {};
  $scope.repair.head = "Please wait";
  $scope.webService = webService;
  $scope.$watch('webService.repair', function(newVal){
    console.log("Hello I am updated");//it displayed on console
    console.log($scope.webService); //this comes undefined
    console.log(newVal);//this comes undefined
    $scope.repair = $scope.webService.repair;
  });

我能够观看webService的修复变量,但是我无法获得更新的值。我提到this

由于

0 个答案:

没有答案