简体   繁体   English

AngularJS在多个控制器中使用函数

[英]AngularJS Using Function in Multiple Controllers

Code in Controller: 控制器中的代码:

    var type = null;
    var title = null;
    var content = null;

    function showMessage(type, title, content) {
      $scope.displayMessage = true;
      $scope.message = {
        type: type,
        title: title,
        content: content
      }
      $timeout(function() {
          $scope.fadeMessageSuccess = true;
      }, 3000);
    };

    var type = "success";
    var title = "Thanks for registering!";
    var content = "Your account has successfully been created!";
    showMessage(type, title, content);

The above is my code that I'm working with and it's inside a controller currently. 上面是我正在使用的代码,当前在控制器内部。 It works perfectly however I want to clean it and up and use it in multiple controllers. 它工作完美,但是我想清理它并在多个控制器中使用它。 How do I go about wrapping this part in a function to be used throughout the app and then only having to call the last 4 lines in my controllers: 我该如何将这部分包装到要在整个应用程序中使用的函数中,然后只需要在控制器中调用最后4行:

    var type = null;
    var title = null;
    var content = null;

    function showMessage(type, title, content) {
      $scope.displayMessage = true;
      $scope.message = {
        type: type,
        title: title,
        content: content
      }
      $timeout(function() {
          $scope.fadeMessageSuccess = true;
      }, 3000);
    };

I only want to have to write the following code whenever I want to show a message: 每当我想显示一条消息时,我只想编写以下代码:

var type = "success";
var title = "Thanks for registering!";
var content = "Your account has successfully been created!";
showMessage(type, title, content);

The View: 风景:

<div ng-controller="AccountCtrl" ng-cloak="">
  <div class="ui {{message.type}} message message-overwrite"
       ng-class="{'fade': fadeMessageSuccess} "
       ng-show="displayMessage">
    <div class="header">
      {{message.title}}
    </div>
    <p>{{message.content}}</p>
  </div>
</div>

As the Angular controller docs suggest, if you want to share code or state across multiple controllers use a service. 如Angular 控制器文档所建议,如果要在多个控制器之间共享代码或状态,请使用服务。

With the addition of one argument you can use a MessageService as a utility. 通过添加一个参数,您可以将MessageService用作实用程序。 You controllers would obviously implement the common functionality which would be the case anyway. 您的控制器显然会实现通用功能,无论如何都会如此。

 angular.module('app', []); angular.module('app') .factory('MessageService', function() { return { showMessage: function(ctrl, type, title, content) { ctrl.displayMessage = true; ctrl.message = { type: type, title: title, content: content } //... } }; }) .controller('First', ['MessageService', function(MessageService) { var self = this; self.displayMessage = false; self.message = {}; self.firstSrvc = function() { MessageService.showMessage(self, "type", "title", "content"); }; } ]) .controller('Second', ['MessageService', function(MessageService) { var self = this; self.displayMessage = false; self.message = {} self.secondSrvc = function() { MessageService.showMessage(self, "type2", "title2", "content2"); }; } ]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <div ng-controller="First as f"> <div ng-controller="Second as s"> <h3>First: {{f.displayMessage}}</h3> <div ng-show="f.displayMessage"> <h3>{{f.message.type}}, {{f.message.title}}, {{f.message.content}}</h3> </div> <h3>Second: {{s.displayMessage}}</h3> <div ng-show="s.displayMessage"> <h3>{{s.message.type}}, {{s.message.title}}, {{s.message.content}}</h3> </div> <div> <button ng-click="f.firstSrvc()">First</button> <button ng-click="s.secondSrvc()">Second</button> </div> </div> </div> </div> 

Simple answer: service .Wrap all your app logic in services. 简单的答案: 服务。将所有应用程序逻辑包装在服务中。 Then you could simply injected in any controller and use it anywhere in your app. 然后,您可以简单地注入任何控制器,并在应用程序中的任何位置使用它。

Since this case doesn't have a lot of logic and It's really conected to the view then you could make a custom directive. 由于这种情况没有太多的逻辑,而且确实是针对视图的,因此您可以制定一个自定义指令。

One more thing if you are trying to make a tosatr there are tones of already implemented ones. 如果您想做一个tosatr,还有一件事,那就是已经实施的提示音。 Don't reinvent the wheel. 不要重新发明轮子。

I hope that helps. 希望对您有所帮助。 Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM