我想在LoginController下调用Homecontroller。 下面是两个控制器的代码:
//Login controller app.controller('LoginController', function ($scope, $cookieStore) { $cookieStore.put('email','test@gmail.com'); // need to call Homecontroller here }); // Home controller app.controller('HomeController', function ($scope, $cookieStore, $location) { if ($cookieStore.get('email') != null) { $scope.wecomeMessage = $cookieStore.get('email'); } else { $scope.wecomeMessage = ""; } });i want to call Homecontroller under the LoginController. below is the code of both controller:
//Login controller app.controller('LoginController', function ($scope, $cookieStore) { $cookieStore.put('email','test@gmail.com'); // need to call Homecontroller here }); // Home controller app.controller('HomeController', function ($scope, $cookieStore, $location) { if ($cookieStore.get('email') != null) { $scope.wecomeMessage = $cookieStore.get('email'); } else { $scope.wecomeMessage = ""; } });最满意答案
我认为你误解了角度控制器的责任。
如何使用angular docs中的控制器:
使用控制器来:
设置$ scope对象的初始状态。
向$ scope对象添加行为。
不要使用控制器:
操纵DOM - 控制器应仅包含业务逻辑。 将任何表示逻辑放入控制器会显着影响其可测试性。 Angular对大多数情况和指令进行数据绑定以封装手动DOM操作。 格式输入 - 改为使用角度形式控件。 滤镜输出 - 改为使用角度滤镜。 跨控制器共享代码或状态 - 改为使用角度服务。 管理其他组件的生命周期(例如,创建服务实例)。
因此,如果您想创建一些可重用代码块,您只需要创建一个角度服务,其中包含具有所需逻辑的函数。 然后你可以将它注入两个控制器,它们将具有必要的功能。
例:
app.controller('LoginController', function ($scope, $cookieStore, helperService) { $cookieStore.put('email', 'test@gmail.com'); $scope.wecomeMessage = helperService.getMessage(); }); // Home controller app.controller('HomeController', function ($scope, $cookieStore, $location, helperService) { $scope.wecomeMessage = helperService.getMessage(); }); app.service('helperService', function ($cookieStore) { this.getMessage = function () { return $cookieStore.get('email') != null ? $cookieStore.get('email') : ""; }; });I think you misunderstood the responsibility of controllers in angular.
How to use controllers from angular docs:
Use controllers to:
Set up the initial state of the $scope object.
Add behavior to the $scope object.
Do not use controllers to:
Manipulate DOM — Controllers should contain only business logic. Putting any presentation logic into Controllers significantly affects its testability. Angular has databinding for most cases and directives to encapsulate manual DOM manipulation. Format input — Use angular form controls instead. Filter output — Use angular filters instead. Share code or state across controllers — Use angular services instead. Manage the life-cycle of other components (for example, to create service instances).
So if you want to create some block of reusable code, you just need to create an angular service, which will include function with needed logic. Then you can inject it to both controllers and they will have necessary functionality.
Example:
app.controller('LoginController', function ($scope, $cookieStore, helperService) { $cookieStore.put('email', 'test@gmail.com'); $scope.wecomeMessage = helperService.getMessage(); }); // Home controller app.controller('HomeController', function ($scope, $cookieStore, $location, helperService) { $scope.wecomeMessage = helperService.getMessage(); }); app.service('helperService', function ($cookieStore) { this.getMessage = function () { return $cookieStore.get('email') != null ? $cookieStore.get('email') : ""; }; });更多推荐
发布评论