简体   繁体   English

AngularJS UI Bootstrap模式共享信息到同一控制器

[英]Angularjs ui bootstrap modal sharing information to same controller

I have the following modal set up in my cloud controller. 我在cloud控制器中设置了以下模式。 I'm not giving this modal a specific controller to work off of because the goal is to share it with my cloud controller. 我没有为此模式提供特定的控制器,因为目标是与我的云控制器共享。

I don't tell the modal that it should be using the cloud controller because then the controller will get run twice, and I do not want this to happen. 我没有告诉模态它应该使用云控制器,因为这样控制器将运行两次,并且我不希望这种情况发生。

.config(function($stateProvider, $urlRouterProvider, $locationProvider) {

    $stateProvider
        .state('cloud', {
            url: '/cloud',
            controller: 'cloud',
            templateUrl: 'pages/templates/cloud.html'
        })
})

.controller('cloud', function($scope, $rootScope, $http, $state, $stateParams, $modal) {

    $scope.toggleModal = function () {
        $scope.renameModal = $modal.open({
            animation: true,
            templateUrl: 'pages/templates/modal.html',
            size: "md",
            scope: $scope
        });
    }

    $scope.submit = function(data) {
        console.log($scope.inputData, data);
    }

})

The issue I am trying to solve is with an input box in my modal template. 我要解决的问题是模态模板中的输入框。 I am trying to submit the text, have it be shared, and hopefully updated to one of my $scope variables inside the cloud controller. 我正在尝试提交文本,进行共享,并希望将其更新为云控制器内部的$scope变量之一。

Below you can see my modal.html where on submit, it runs the submit() function in my cloud controller. 在下面,您可以看到我的modal.html提交时,它将在我的云控制器中运行Submit submit()函数。 It run's successfully but the console log returns undefined for both $scope.inputData and data . 它运行成功,但控制台日志为$scope.inputDatadata返回未定义

<div class="modal-header">
    <h3 class="modal-title">title</h3>
</div>
<div class="modal-body">
    <input type="text" id="rename_item" ng-modal="inputData" value="" />     
</div>
<div class="modal-footer">
    <button class="btn_submit fade" ng-click="submit(inputData)">Rename</button>
</div>

Could anyone help me figure out what I am doing wrong or how I can get this data over to my current cloud controller? 谁能帮助我弄清楚我做错了什么,或者如何将这些数据转移到当前的云控制器上?

A BIG shoutout to camden_kid for catching my mistake of misspelling ng-model . 一个大的大喊答题节目环节以camden_kid追赶我的拼写错误的错误ng-model I accidentally did ng-modal . 我不小心做了ng-modal

Just correcting that spelling didn't fix it though. 只是纠正拼写并不能解决它。

I don't know why (maybe someone can help explain), but I had to use an object that was already created to get this to work. 我不知道为什么(也许有人可以帮忙解释),但是我不得不使用已经创建的对象才能使它工作。 Just using a variable that was pre-defined didn't work. 仅使用预定义的变量不起作用。

So this is my change in my cloud controller: 这是我对云控制器的更改:

.controller('cloud', function($scope, $rootScope, $http, $state, $stateParams, $modal) {

    $scope.toggleModal = function () {
        $scope.renameModal = $modal.open({
            animation: true,
            templateUrl: 'pages/templates/modal.html',
            size: "md",
            scope: $scope
        });
    }

    $scope.inputText = {data: ""};
    $scope.submit = function(data) {
        console.log($scope.inputText.data);
    }

})

And then I did the following to my html: 然后我对html进行了以下操作:

<input type="text" ng-modal="inputText.data" />  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM