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;
});
}
}
}
scope, but you are injecting$scope. the$is essential