$watch not updating subview

Nick

I have this directive that changes $scope.mode in the parent $scope:

angular.module('Selector', []).directive('mySelector', function() {
  var changeMode;
  changeMode = function(newmode) {
    var $scope;
    $scope = this;
    $scope.mode = newmode;
    $('.menu-open').attr('checked', false);
    return $scope.mode;
  };
  return {
    restrict: 'E',
    scope: {
      mode: '=mode',
      id: '@id'
    },
    replace: true,
    templateUrl: './directives/modeSelector/Selector.tpl.html',
    link: function(scope, element, attrs) {
      scope.changeZoneMode = changeMode;
      return scope.$watch((function() {
        return scope.mode;
      }), function(newMode) {
        return scope.mode = newMode;
      });
    }
  };
});

this directive is included into main.html, as well as a ui-view that loads a subview:

 <my-selector mode="current.Mode" id="{{current.ID}}"></my-selector>
 <!-- This binding works and updates properly -->
 {{current.Mode}}

 <!-- Subview container -->
 <div ui-view="sub"></div>

subviewTemplate.html:

<!-- This binding doesn't work! -->
{{current.Mode}}

the subview doesn't have a specific controller, and it uses the parent one, as from the settings in app.js:

.state('app.details', {
  name: 'appDetails',
  url: '/:zoneID',
  views: {
    'appContent': {
      templateUrl: 'main.html',
      controller: 'ctrl'
    }
  }
}).state('app.details.overview', {
  name: 'appDetailsOverview',
  url: '/details',
  views: {
    'appContent': {
      templateUrl: 'main.html',
      controller: 'ctrl'
    },
    'sub': {
      templateUrl: 'subviewTemplate.html',
      controller: 'ctrl'
    }
  }
});

and the controller used by both main.html and subviewTemplate.html:

angular.module('myController', ['services']).controller('ctrl', [
  '$scope', 'Data', function($scope, Data) {

    $scope.current = new Data;
    $scope.current.load();

    return $scope.$watch('current.Mode', (function(newValue, oldValue) {
      console.log('Old value is: ' + oldValue);
      $scope.current.Mode = newValue;
      return console.log('new value is: ' + $scope.currentMode);
    }), true);
  }
]);

I don't understand why it works on main.html, updating properly, but not on subviewTemplate.html. console.log inside $watch prints the right value.

Any help? What Am I doing wrong here?

carlcheel

You're initializing a new instance of the ctrl controller. Remove that line and it'll use the parent controller which will be ctrl. For example:

'sub': {
    templateUrl: 'subviewTemplate.html'
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular $watch not updating bindings

From Dev

$watch not updating scope variable

From Dev

setNeedsDisplay() is not updating collectionViewCell subView's drawRect

From Dev

Swift UITableViewCell Subview Layout Updating Delayed

From Dev

Swift UITableViewCell Subview Layout Updating Delayed

From Dev

Apple Watch Complication not updating in background

From Dev

Angular JS - Scope Watch Not Updating

From Dev

$watch() isn't updating the $scope

From Java

Why is webpack watch not updating my code?

From Dev

What is the flow for updating complication data for Apple Watch?

From Dev

UIStackView not updating after updating height of subview (UITableView with no scroll) while inside a ScrollView

From Dev

Why is webpack --watch not updating when specific files are updated

From Dev

Angular ui.bootstrap pagination - current page not updating/watch not working

From Dev

$scope.$watch on an array is not updating when the values are added from a directive

From Dev

$scope.$watch on an array is not updating when the values are added from a directive

From Dev

Issue while updating value of Angular Directive's model while updating from watch of form

From Dev

Unable to watch variables during debug of T4 templates after updating to Visual Studio 2015 Update 2

From Dev

SFSafariViewController as subview

From Dev

add subView to another SubView ios

From Dev

iOS : Resizing subview by removing a subview

From Dev

SubView hierarchies not working (SubView hidden below another)

From Dev

Wait for subview to be displayed, then process, then remove subview

From Dev

How to bring a subview's subview to top?

From Dev

subView of UIPageViewController not animating Subview? Swift 4

From Dev

IOS/Objective-C: Subview of Subview not displaying

From Dev

UIButton in SubView of ScrollView not working

From Dev

CAShapeLayer subview not animating correctly

From Dev

iOS subview custom transition

From Dev

iOS bring subview to back

Related Related

  1. 1

    Angular $watch not updating bindings

  2. 2

    $watch not updating scope variable

  3. 3

    setNeedsDisplay() is not updating collectionViewCell subView's drawRect

  4. 4

    Swift UITableViewCell Subview Layout Updating Delayed

  5. 5

    Swift UITableViewCell Subview Layout Updating Delayed

  6. 6

    Apple Watch Complication not updating in background

  7. 7

    Angular JS - Scope Watch Not Updating

  8. 8

    $watch() isn't updating the $scope

  9. 9

    Why is webpack watch not updating my code?

  10. 10

    What is the flow for updating complication data for Apple Watch?

  11. 11

    UIStackView not updating after updating height of subview (UITableView with no scroll) while inside a ScrollView

  12. 12

    Why is webpack --watch not updating when specific files are updated

  13. 13

    Angular ui.bootstrap pagination - current page not updating/watch not working

  14. 14

    $scope.$watch on an array is not updating when the values are added from a directive

  15. 15

    $scope.$watch on an array is not updating when the values are added from a directive

  16. 16

    Issue while updating value of Angular Directive's model while updating from watch of form

  17. 17

    Unable to watch variables during debug of T4 templates after updating to Visual Studio 2015 Update 2

  18. 18

    SFSafariViewController as subview

  19. 19

    add subView to another SubView ios

  20. 20

    iOS : Resizing subview by removing a subview

  21. 21

    SubView hierarchies not working (SubView hidden below another)

  22. 22

    Wait for subview to be displayed, then process, then remove subview

  23. 23

    How to bring a subview's subview to top?

  24. 24

    subView of UIPageViewController not animating Subview? Swift 4

  25. 25

    IOS/Objective-C: Subview of Subview not displaying

  26. 26

    UIButton in SubView of ScrollView not working

  27. 27

    CAShapeLayer subview not animating correctly

  28. 28

    iOS subview custom transition

  29. 29

    iOS bring subview to back

HotTag

Archive