0

I am building an angularjs app using typescript, and have just created my first controller - the problem is that I'm having a hard time using ng-model with an object on it.

This is what it all looks like;

app.ts

((): void => {
   'use strict';
   angular.module('app', ['app.core','app.widgets','app.services']);
})();

app.services.ts

User Service is defined here and is not relevant to this sample

'app.widgets.ts`

module app.widgets {
   'use strict';
   interface IUserController {
      User: app.services.IUser;
   }

   class UserController implements IUserController {
      User: app.services.IUser;

      static $inject = ['$scope', 'app.services.UserService'];
      constructor(scope: any, userService: app.services.IUserService){
         var vm = this;
         userService.Find().then((user: app.services.IUser): void => {
            scope.User = user; // this is the offending line
         });
      }
   }

   angular.module('app.widgets').controller('app.widgets.UserController', UserController);
}

I attempt to call this in my HTML like this;

<body ng-app="app" ng-controller="app.widgets.UserController">
   {{ User.UserName }}
</body>

Or ..

<h1 ng-model="User.UserName"></h1>

Update

I haven't quite solved it completely, but I have made more progress

interface IUserControllerScope extends angular.IScope {
   User: app.services.IUser;
}

class UserController {
    static $inject = ['$scope', 'app.services.UserService'];
    constructor($scope: IUserControllerScope, userService: app.services.IUserService) {
       var _this = $scope;
       userService.Find().then((user: app.services.IUser): void => {
             _this.User = user;
          });
       }
    }
}
2
  • your constructor is asking for scope, but you are injecting $scope. the $ is essential Commented Mar 21, 2015 at 20:21
  • I tried that, it did not work either. Commented Mar 22, 2015 at 0:14

3 Answers 3

1

I think the problem is because scope is undefined. Try changing scope to $scope. Then you can access user like this

$scope.User = user
Sign up to request clarification or add additional context in comments.

2 Comments

I actually made a bit of progress with that, though it wasn't the sole answer.
The answer turned out to be that I needed to both declare $scope and I also needed the var _this = $scope so that I could add things to it. I had to make an interface that extended angular.IScope to do this and get type safety.
1

{{ User.UserName }}

You might have a typo in this line. Try

<pre> {{User | json}} </pre>

I suspect it should be User.username

1 Comment

UserName is correct. It is from the Microsoft ASP.NET Identity Framework
1

on neat way of doing is not to play with $scope more rather use a instance of controller to assign into a scope variable

class UserController implements IUserController {
  User: app.services.IUser;
  public user:any;
  static $inject = ['$scope', 'app.services.UserService'];
  constructor(scope: any, userService: app.services.IUserService){
     userService.Find().then((user: app.services.IUser): void => {
        this.User = user; // this is the offending line
     });

   //assign instance to a scope variable and access in view
   scope.vm=this
  }
 }

 angular.module('app.widgets').controller('app.widgets.UserController',      UserController);
}

and in view example

   <div>{{vm.user}}</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.