AngularJS 从控制器打开模态

2024-01-03

我是 angularjs 的新手,正在编写一个应用程序,其中包含网格的模板(inventory.html)用 打开,它在网格底部有一个验证按钮,它调用 validate() 来验证选定的网格行,在此之前我需要一个模式弹出并以用户名作为输入,然后进一步处理。

我的验证函数位于模板的控制器内(即 inventory.html)。 我有一个包含路由信息的 app.js 和包含所有模板的控制器的controller.js。

我的问题是,是否有任何方法可以从 validate() 打开模态,获取用户输入并进一步继续,而无需为模态编写单独的控制器。 (我有一个单独的 userinput.html 用于模式。)

(抱歉,如果我的问题不清楚。请帮助我,我一直坚持这个问题,并尝试了网上的许多选项)

// 这是我的控制器

var app = angular.module('app', []);
app.controller('InventoryCtrl',['$scope','$location','$http','$modal',function($scope, $location, $http,$modal{console.log("Inside inventory ctrlr");

 // Validate Function for validate button click
 $scope.validate = function() 
    {   
        $scope.user = null;
        $scope.build = null;
        // Show modal to take user inputs for environment
        var modalInstance = $modal.open(
        {
            controller : "inputModalCntrl",
            templateUrl : "../partials/addEnvironment.html",
            resolve: {
                $callback: function () {
                    return function(param,user,build){
                        /* This function print value(param) dispached in modal controller */
                        console.log(param,user,build);
                                    $scope.user = user;
                                    $scope.build = build;
                    };
                },
                $send: function(){
                    return function(){
                        /* This function send param for the modal */
                        return {param1:"Hello Word Main"};
                    };
                }
            }
        });
// This is further process of function
     postdata = {};
     var dlist = $scope.gridApi.selection.getSelectedRows();
        postdata['dlist'] = dlist;
     $http({ url: "/api/check",  data: postdata, method: "POST" })
    .success(function (response) { alert(response);})
    .error(function () { alert('Error getting data');});
 };
   }]);

这是模态控制器

app.controller("inputModalCntrl", function($scope, $modalInstance, $callback, $send) {
$scope.init = function(){
    /* This function print value(param) dispached in main controller */
    console.log($send);

    /* This send data to main controller */
    $callback({param1:"Hello Word Modal", user :user, build : build});
};
});

我强烈建议你为你的模态编写一个单独的控制器。这将极大地帮助您以后维护代码。

如果我理解正确(从你的标签),你使用这个:https://angular-ui.github.io/bootstrap/ https://angular-ui.github.io/bootstrap/从主控制器打开模式:

var modalInstance = $modal.open({
      animation: $scope.animationsEnabled,
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      size: size,
      resolve: {
        items: function () {
          return $scope.items;
        }
      }
    });

注意解析函数,您可以将这样的额外参数传递给模态控制器。

在模态控制器中:

angular.module('your.awesomeApp').controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {

请注意第三个参数,它将是您传递的值。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AngularJS 从控制器打开模态 的相关文章

随机推荐