Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Binding seems to be not working when a controller written in typescript and called by the directive. this is the directive

import controller = require('framework/frameworkController');

export class FrameworkDirective implements ng.IDirective {
 static instance(): ng.IDirective {
     return new FrameworkDirective;
 }
 controller = 'frameworkController';
 scope: any = {
    title: '@',
 };
  templateUrl = "controlslibrary/framework/frameworkTemplate.html"
}

this is the controller

 export class FrameworkController implements IFrameworkController {
      public isMenuButtonVisible: boolean = true;
      constructor(public $rootScope: ng.IRootScopeService, public $window: ng.IWindowService){
              var fc = this;
            $($window).on('resize.framework', () => {
                   $rootScope.$apply(() => {
                   fc.checkWidth();
                    });
            });
      }

      public checkWidth() {
           var width = Math.max($(this.$window).width(), this.$window.innerWidth);
           this.isMenuButtonVisible = !(width < 768);
      }
 }

this is the template url

 <div ng-if="isMenuButtonVisible">
      <button type="button" class="btn nav-button">
              <i class="fa fa-bars"></i>
      </button>
 </div>

finally this is my module

  import controller = require('framework/frameworkController');
  import directive = require('framework/frameworkDirective');

  angular.module("framework", ['ui.router'])
   .controller("frameworkController", controller.FrameworkController)
   .directive("framework", directive.FrameworkDirective.instance);

when the window is resized it has to check and bring up the button. I can step through the code and it throws no error but the binding is broken. what am i doing wrong?

share|improve this question
    
I would change the controller to inject $scope, then use $scope.$on('resize.framework',()=>{...code...}). Not sure if that will work, but I would test it as a starting point. Would you have your code in a gist so that I could fiddle with it? – Hugues Stefanski May 9 '15 at 7:06
    
resize.framework fires and works just fine, It is the binding that is not working. Should I use $scope to binding to view for controllers, just for directives? the code is in my office network, gist is not allowed. – Bhaskar May 9 '15 at 16:30
    
It seems like there is no "isMenuButtonVisible" on the scope of the directive itself. I am not sure if the following will work as I have never tried it this way before: Have you tried adding a link function to the directive to get access to the controller in the directive: link = (scope, element: ng.IAugmentedJQuery, attrs: any, controller: controller.FrameworkController) => { scope.isMenuButtonVisible = controller.isMenuButtonVisible; } – ZeroOne May 9 '15 at 22:35
    
You sure return new FrameworkDirective; does not give you syntax error? – billc.cn May 10 '15 at 0:22
    
Maybe injecting $scope in the controller and using $scope.$apply instead of $rootScope.$apply (not sure though). Also, put a break in your code inside checkWidth method. I think this might be referring to the window and not the controller, and hence the properties might not be what you think. – Hugues Stefanski May 10 '15 at 7:27

thanks for the comments. I had to include isMenuButtonVisible as a property to $scope to make it work. it wouldn't bind unless it is a part of $scope.

 interface IFrameworkControllerScope extends ng.IScope {
       isMenuVisible: boolean;
 }

 export class FrameworkController 
 {
   $scope.isMenuButtonVisible = true;
   constructor({public $scope : IFrameworkControllerScope,  public $window: ng.IWindowService){

   $($window).on('resize.framework', () => {
        $scope.$apply(() => {
            checkWidth();
        });
    });

   var checkWidth = () =>{
        var width = Math.max($(this.$window).width(), this.$window.innerWidth);
        $scope.isMenuButtonVisible = !$scope.isMenuVisible;
    }
 }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.