Javascript 在AngularJS中表单中有多个下拉列表时的最佳实践

Javascript 在AngularJS中表单中有多个下拉列表时的最佳实践,javascript,angularjs,Javascript,Angularjs,当您在表单中有多个下拉列表并希望向其中添加数据时,您是使用单独的控制器还是将它们全部放在一个控制器下 .controller("Controller1", ['$scope', function ($scope) { $scope.dd1 = [ { id: 1, type: 'test1' }, { id: 2, type: 'test2' } ]; $scope.dd2 = [ { id: 1, type: 'test3

当您在表单中有多个下拉列表并希望向其中添加数据时,您是使用单独的控制器还是将它们全部放在一个控制器下

.controller("Controller1", ['$scope', function ($scope) {
    $scope.dd1 = [
        { id: 1, type: 'test1' },
        { id: 2, type: 'test2' }
    ];
    $scope.dd2 = [
        { id: 1, type: 'test3' },
        { id: 2, type: 'test4' }
    ];
}]);


OR

.controller("Controller1", ['$scope', function ($scope) {
    $scope.dd1 = [
        { id: 1, type: 'test1' },
        { id: 2, type: 'test2' }
    ];

.controller("Controller2", ['$scope', function ($scope) {
    $scope.dd2 = [
        { id: 1, type: 'test3' },
        { id: 2, type: 'test4' }
    ];

视情况而定。Angular是关于控制器之间关注点的分离。从应用程序的角度来看,如果下拉列表是相关的,我建议将它们放在同一个控制器下。您不希望在Angular应用程序中创建太多的控制器,因为这会很快使应用程序的可维护性变得复杂

这取决于下拉列表的用法和其中的元素数。 如果它是一个重要的菜单,或者您想在它上面进行更多的交互(添加指令或其他依赖于它的元素),您应该拆分它们。
但是,如果您只想显示数据,并添加/删除菜单选项卡,请将其放在同一个控制器中。

它只是一个普通表单,有两个文本框和三个下拉列表,我稍后将发布。没有指令。那么,对于这个简单的表单,我应该只使用一个控制器?我会将它们放在同一个控制器中开始-如果应用程序变得更大,那么我会相应地重构代码。单个控制器使开发更容易开始。