AngularJS用户界面模式

AngularJS用户界面模式,angularjs,Angularjs,我有一个注册表格在模态里面。在模式中提交表单时,我希望将保存的项推送到数组$scope.lists=[]中。$scope.lists在ListsController中,我在ModalsController中有submit函数 angular.module('app') .controller('ListsController', function ($scope, listsRegister, $uibModal) { $scope.lists = []; $scope.ope

我有一个注册表格在模态里面。在模式中提交表单时,我希望将保存的项推送到数组$scope.lists=[]中。$scope.lists在ListsController中,我在ModalsController中有submit函数

angular.module('app')
.controller('ListsController', function ($scope, listsRegister, $uibModal) {
    $scope.lists = [];

    $scope.openFormListModal = function () {
        var modalInstance = $uibModal.open({
            ...,
            controller: function ($scope, $uibModalInstance) {                  

                // Submiting the form
                $scope.submit = function () {

                    listsRegister.register($scope.list)                     
                        .then(function (response) {
                            if (response.insert) {
                                $scope.form.$setPristine();
                                $scope.form.$setUntouched();
                                $scope.list = {};

                                // $scope.lists.push(response); <- Is it possible to access the lists from the ListsController inside this Modal's Controller?
                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                };
            },
            size: 'sm',
        });
    };
});
angular.module('app')
.controller('ListsController',函数($scope,listsRegister,$uibModal){
$scope.lists=[];
$scope.openFormListModal=函数(){
var modalInstance=$uibModal.open({
...,
控制器:函数($scope,$uibModalInstance){
//提交表格
$scope.submit=函数(){
listsRegister.register($scope.list)
.然后(功能(响应){
if(response.insert){
$scope.form.$setPristine();
$scope.form.$setUntouched();
$scope.list={};

//$scope.lists.push(response);您可以通过以下方式将值传递到ui模式:

var modal= $modal.open({
      templateUrl: '...',
      controller: '...',
      resolve: {
         myList: function () {
           return $scope.lists;
         }
      }
});

然后,您将能够访问控制器中的myList,您可以通过以下方式将值传递给ui模式:

var modal= $modal.open({
      templateUrl: '...',
      controller: '...',
      resolve: {
         myList: function () {
           return $scope.lists;
         }
      }
});

然后,您将能够访问控制器中的myList,这是直接从文档中获取的:

scope(Type:$scope)—要用于 modal的内容。默认为$rootScope


这直接来自文档:

scope(Type:$scope)—要用于 modal的内容。默认为$rootScope


请注意,您必须小心引用的$scope。Angular为作用域定义了一个层次结构,在本例中,您使用的两个作用域在此层次结构中是父作用域和子作用域。您共享的每个控制器都有自己的$scope对象。要更清楚地了解此主题,可以查看:

尽管如此,看起来您希望将一些数据从子$scope填充到父$scope。为此,“您需要在父范围中使用对象(而不是原语),然后您将能够直接从子范围中更新它”。这是中接受的答案的一部分。请签出


此外,我建议在控制器中使用“vm”符号,以保护$scope对象。它在$scope上提供了一个层,您可以在其中执行所有工作。这是John Papa(Angular的专家)推荐的一种良好做法。您可以在中获得更多信息,请注意,您必须小心引用的$scope。Angular为作用域定义了一个层次结构,在本例中,您使用的是两个作用域,即此层次结构中的父作用域和子作用域。您共享的每个控制器都有自己的$scope对象。为了更清楚地了解您可以查看的主题:

尽管如此,看起来您希望将一些数据从子$scope填充到父$scope。为此,“您需要在父范围中使用对象(而不是原语),然后您将能够直接从子范围中更新它”。这是中接受的答案的一部分。请签出

此外,我建议在控制器中使用“vm”符号,以保护$scope对象。它在$scope上提供了一个层,您可以在其中执行所有工作。这是John Papa(Angular的导师)推荐的一种良好做法。您可以在和中获得更多信息