重用控制器

时间:2017-07-20 06:29:49

标签: javascript angularjs inheritance

我想重用控制器之间定义的代码

.controller('GenericController', ['$scope', '$controller', '$rootScope', '$dialogs', '$state', '$http', '$modal', '$q', '$timeout', 'projectFactory', 'projectPromise', 'phaseFactory', 'buFactory', 'stakeholderGroupFactory', 'ldapFactory', 'genericFactory', 'User',

    function ($scope, $controller, $rootScope, $dialogs, $state, $http, $modal, $q, $timeout, projectFactory, projectPromise, phaseFactory, buFactory, stakeholderGroupFactory, ldapFactory, genericFactory, User) {

      $scope.testing = function() {
        console.log("Hello");
      };
}]);

2 个答案:

答案 0 :(得分:3)

您可以使用工厂并为函数创建对象以重复使用它。

app.factory("sample",function(){
    return function() {
        console.log("Hello");
      };
})

其他整理多个常用功能

app.factory("commonFunctions",function(){

    commonFunction1(){
       console.log("common func1")
    } 

    commonFunction2(){
       console.log("common func2")
    } 

   return {
         commonFunction1: commonFunction1,
         commonFunction1: commonFunction2
   };
})

答案 1 :(得分:0)

使用$controller导入此修复问题