【问题标题】:is it Possible (partial class) concept for controller in angularjs?angularjs中控制器的可能(部分类)概念吗?
【发布时间】:2016-02-04 12:28:47
【问题描述】:

有没有办法在 angularjs 中使用部分代码进行代码分组?

原因 --- 我有一个包含太多代码的控制器。这个控制器包含了一些方法和许多功能的代码,它降低了代码的可读性。我想保持控制器名称相同并进行代码分组。

例如:-

app.controller('MainController', function ($scope, router) {
    $scope.Read = function() {

    };
    $scope.Write = function() {

    };
    $scope.Update = function() {

    };
    $scope.Delete = function() {

    };
});

部分:

    app.controller('MainController', function ($scope, router) {
        $scope.firstName = firstname;
        $scope.middleName = middleName;
        $scope.lastName = lastName;
    });

另一个部分:

    app.controller('MainController', function ($scope, router) {
        $scope.Print = function() {

        };
        $scope.PrintPreviw = function() {

        };
    });

我需要这样实现。

还有其他方法吗?是的,请更新.. 谢谢

【问题讨论】:

  • @SureshB 你的意思是将函数分离到它们自己的控制器或类中吗?
  • 是的,在单控制器中我有很多 fn 和变量.. 所以我想对代码进行分组。
  • 拥有一个控制器来做很多事情以至于您需要将其拆分为多个文件通常是一种不好的形式。我建议拥有多个控制器(使用指令或 ui-router 和多个视图)和/或将控制器中的任何功能代码分解为服务。

标签: javascript angularjs angularjs-directive angularjs-scope angularjs-controller


【解决方案1】:

您可以尝试将代码分类为逻辑块。之后,您可以将逻辑块合并到对象中。

//Do NOT do this!
$scope.firstName = firstname;
$scope.middleName = middleName;
$scope.lastName = lastName;

//instead do this
var person = {
    firstname: "name",
    middleName: "mName",
    lastName: "Lname"
}

//then in your view access them like this
{ { person.firstname } }

此外,您可以将一些代码移动到服务或工厂中。然后在控制器中使用这些方法。

//take things out of your controller and put then in a service or factory.
app.service('notePad', function () {
    return {

        Read: function (newState) {
            return state += state;
        },

        Write: function (state) {
            return state += state;
        },
        Update: function (state) {
            state = state;
        },
        Delete: function () {
            state = null;
        }

    };
});

//then in your controller access them like this
//Note you do not need everything on $scope if you are not going to use it in the view.
app.controller('MainController', function ($scope, router, notePad) {
    $scope.Read = notePad.Read();
    $scope.Write = notePad.Write();
    $scope.Update = notePad.Update();
    $scope.Delete = notePad.Delete();
});

如果您需要服务内的控制器功能,您仍然可以访问范围和其他控制器属性,例如。

注意:确保您创建了一个 onDestroy 方法来清理所有剩余的变量。

//pass your scope to your service
app.controller('MainController', function ($scope, router, notePad) {
    notePad.changeView($scope);
});

//munipulate your scope just like you would in a controller
app.service('notePad', function () {
    function changeView(scope) {
        scope.write = 'example';
    }
});

//the result of the scope change in the service will show up in the view
{ { write } } == 'example'

【讨论】:

  • 嗨,马特,你所解释的是正确的,我们对我这样放置的样本做同样的事情。即使在控制器中很多 fn thr 也可能是部分控制器?
  • 我添加了一个在控制器之外进行控制器工作的示例。在控制器中完成的大多数事情也可以在控制器之外完成。我不确定如何将控制器分成两部分,但我知道您可以让控制器在控制器之外工作。这将允许您拆分当前控制器的工作流程。我希望这会有所帮助!
猜你喜欢
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 2015-01-11
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多