1

I made AngularJs app by typescript. and use requireJs load 'js(compiled ts) file'

I made simple controller A

myCtrl.ts

module myApp {
 'use strict';
 interface myMessage extends ng.IScope {
 message: string;
}
export class myCtrl {
 constructor($scope: myMessage) {
   $scope.message = "hello world";
  }
 }
}

and I load myApp file in main.ts

myApp.ts

import myController = require('./myCtrl');
module myApp{
'use strict';
angular.module('myApp',[]).controller('myCtrl',myCtrl);
}
angular.bootstrap(document,['myAppDo']);

load was success but $scope.message not changed.

'Cannot read property 'myCtrl' of undefined'

when I change myApp.ts it work's fine

angular.module('myApp', []).controller('myCtrl', ($scope, $http) => {
  $scope.message = "hello";
});
angular.bootstrap(document,['myApp']);

how can I Solve?

2 Answers 2

1

I solved it. first, add 'export' the controller module

module myApp 

to

export module myApp 

then specify the path

import myCtrl = require('./myCtrl');
angular.module('myApp',[]).controller('myCtrl',myCtrl);

to

import myCtrl = require('./myCtrl');
angular.module('myApp',[]).controller('myCtrl',myCtrl.myApp.myCtrl);

it seems something is wrong, but it work

Sign up to request clarification or add additional context in comments.

Comments

0

Steps

  1. Add AMD-dependency
  2. Import module

Result code

///<amd-dependency path="here path to your module"/>

import myController = require('myCtrl');

1 Comment

I tried, but myController can't access the myCtrl class too

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.