AngularJs 1.3.x,简单的控制器可以工作,但是一旦我使用Typescript和Injection重新编写它,它就会失败。如果引用1.2.x,它将再次开始工作。
//This works in 1.3.x
scopeApp.controller('MyController', ['$scope', function ($scope) {
$scope.username = 'World';
$scope.sayHello = function () {
$scope.greeting = 'Hello ' + $scope.username + '!';
};
}]);
http://plnkr.co/edit/ssSuuZuGlrypemx3BU5r?p=preview
//This DOES NOT works in 1.3.x but does in 1.2.x
//The following code is produced via Typescript:
var MainFeature;
(function (MainFeature) {
var MainCtrl = (function () {
function MainCtrl($scope) {
this.scope = $scope;
this.name = "Sirar";
this.message = '';
}
MainCtrl.prototype.SetMessage = function () {
this.message = 'Hello' + this.name;
};
return MainCtrl;
})();
MainFeature.MainCtrl = MainCtrl;
})(MainFeature || (MainFeature = {}));
scopeApp.controller("MainCtrl", ["$scope", function ($scope) {
return new MainFeature.MainCtrl($scope);
}]);
具有重要信息但无济于事的重大更改文档:
https://docs.angularjs.org/guide/migration
http://ng-learn.org/2014/06/Migration_Guide_from_1-2_to1-3/
http://wildermuth.com/2014/11/11/Angular_1_3_and_Breaking_Change_for_Controllers
最佳答案
您需要传递构造函数,而不是像您一样传递其他函数。正如我在another answer中所述,不是通过调用new
创建控制器的。它的创建如下:
instance = Object.create(controllerPrototype);
...
return fn.apply(self, args);
要注意的是,不使用返回值,而是使用
instance
。在您的情况下,这意味着:instance = Object.create({}); // should be MainCtrl.prototype
...
return fn.apply(self, args);
因此,“ MainCtrl”最终成为空对象。首先,您必须做应该做的事情,并通过构造函数:
scopeApp.controller("MainCtrl", ["$scope", MainFeature.MainCtrl]);
关于javascript - Angular 1.3重大更改-范围已设置,但在$ apply之前重置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27915321/