5 javascript twitter-bootstrap
submit
单击任一关闭按钮时,我将使用三个参数调用该函数。
<div class="modal" ng-controller="emailViewController">\n <div class="modal-header" >\n <button type="button" class="close" ng-click = "submit(information.add, information.subject, information.emailContent); close(); clear()">\xc3\x97</button>\n <h3>Compose Email</h3>\n </div>\n <div class="modal-body">\n <form >\n <tags options="{addable: true}" typeahead-options="typeaheadOpts" data-model="information.add" data-src="toPerson as toPerson for toPerson in to"></tags>\n <input type="text" placeholder="Subject" style="width:95%;" data-ng-model = "information.subject"><br />\n <textarea style="width:95%;" rows="10" data-ng-model = "information.emailContent"></textarea>\n </form>\n </div>\n <div class="modal-footer">\n <a href="#" class="btn" ng-click = "submit(information.add, information.subject, information.emailContent); close(); clear()">Close</a>\n <a href="#" class="btn btn-primary" ng-click = "close(); clear()">Send</a>\n </div>\n </div>\n
Run Code Online (Sandbox Code Playgroud)\n\n我有以下问题:
\n\n1)当用户单击 之外的任意位置将其关闭时,如何调用submit
具有相同三个参数 ( ) 的函数。information.add, information.subject, information.emailContent
modal
为了避免混淆,当单击按钮时我已经调用了该submit
函数。close
我只是不确定当用户单击外部modal
(而不是关闭按钮)来关闭它时如何调用它。
我尝试使用该hidden.bs.modal
事件,因为它在关闭时触发modal
,但无法让它工作。
$(\'.modal\').on(\'hidden.bs.modal\', function () {\n $scope.submit(add, subject, emailContent);\n }); \n
Run Code Online (Sandbox Code Playgroud)\n\nsubmit
2)当用户关闭浏览器时 如何调用该函数?
尝试1
\n\nsubmit
我意识到当事件触发时我没有在函数中调用正确的参数hidden.bs.modal
,因此我修改了代码如下:
$(\'.modal\').on(\'hidden.bs.modal\', function () {\n $scope.submit($scope.information.add, $scope.information.subject, $scope.information.emailContent)\n });\n
Run Code Online (Sandbox Code Playgroud)\n\n该submit
函数将三个值存储在 中localStorage
。
information.add
上面的代码存储了in的值localStorage
,但没有存储information.subject
和information.emailContent
in的值localStorage
尝试2
\n\nsubmit
功能附加到$scope
该控制器的 ( emailViewController
)
(function() {\n \'use strict\';\n var emailViewController = function (fetchDataService,\n $scope,$filter,$timeout, $localStorage) {\n\n $scope.information = {\n add: [],\n subject: [],\n emailContent: []\n };\n\n $scope.submit = function (add, subject, emailContent) {\n console.log(add);\n console.log(subject);\n console.log(emailContent);\n\n if (! ($localStorage.add instanceof Array) ) {\n $localStorage.add = [];\n }\n\n if(add.length != 0) {\n $localStorage.add.push(add);\n }\n\n if (! ($localStorage.subject instanceof Array) ) {\n $localStorage.subject = [];\n }\n\n if(subject != "") {\n $localStorage.subject.push(subject);\n console.log("after localStorage" + subject);\n }\n\n if (! ($localStorage.emailContent instanceof Array) ) {\n $localStorage.emailContent = [];\n }\n\n if(emailContent != "") {\n $localStorage.emailContent.push(emailContent);\n }\n };\n\n $(\'.modal\').on(\'hidden.bs.modal\', function () {\n $scope.submit($scope.information.add, $scope.information.subject, $scope.information.emailContent)\n });\n\n $scope.localStorage = $localStorage;\n console.log($scope.localStorage);\n };\n\n angular.module(\'iisEmail\')\n .controller (\'emailViewController\',\n [\'fetchDataService\', \'$scope\',\'$filter\', \'$timeout\', \'$localStorage\', emailViewController]);\n}());\n
Run Code Online (Sandbox Code Playgroud)\n\n添加Attempt 1
到控制器后,出现新问题。为了避免这种情况,我必须Attempt 1
从控制器中删除。
这就是发生的问题 -close
单击按钮时,submit
调用。After Attempt 1
,即使在事件被触发submit
时也会被调用。hidden.bs.modal
结果,三个参数的值被存储在 localStorage 中两次。
尝试3
\n\n我尝试使用onBlur
代替hidden.bs.modal
,但事件没有触发。
$(\'.modal\').on(\'blur\', function () {\n console.log("Blurred");\n $scope.submit($scope.information.add, $scope.information.subject, $scope.information.emailContent)\n });\n
Run Code Online (Sandbox Code Playgroud)\n\n尝试4
\n\n当我这样做时console.log($scope)
,我会看到用户在对象中输入的三个参数的值localStorage
。这正是我想要的,但这些值没有存储在Local Storage
. 因此,当我打开开发工具并检查 时Local Storage
,我看不到用户输入的值。
本质上,我能够看到localStorage
对象中的数据,因为我$scope.localStorage = $localStorage
在控制器中执行此操作(emailViewController
在 中Attempt 2
),但我看不到用户在浏览器中输入的数据Local Storage
尝试5
\n\n我不再submit
从视图调用该函数,因为我正在使用该hidden.bs.modal
事件。现在,当我单击关闭按钮时,hidden.bs.modal
将被触发并被Submit
调用。我可以看到用户在localStorage
对象和 devtools 中输入的值Local Storage
。这工作得很好。
当用户单击模态框外部以将其关闭(而不是在按钮上)时,hidden.bs.modal
将被触发并被submit
调用。在这种情况下,我可以看到用户在localStorage
对象中输入的值,但看不到 devtools 中的值Local Storage
。
<div class="modal" ng-controller="emailViewController">\n <div class="modal-header" >\n <button type="button" class="close" ng-click = "close(); clear()">\xc3\x97</button>\n <h3>Compose Email</h3>\n </div>\n <div class="modal-body">\n <form >\n <tags options="{addable: true}" typeahead-options="typeaheadOpts" data-model="information.add" data-src="toPerson as toPerson for toPerson in to"></tags>\n <input type="text" placeholder="Subject" style="width:95%;" data-ng-model = "information.subject"><br />\n <textarea style="width:95%;" rows="10" data-ng-model = "information.emailContent"></textarea>\n </form>\n </div>\n <div class="modal-footer">\n <a href="#" class="btn" ng-click = "close(); clear()">Close</a>\n <a href="#" class="btn btn-primary" ng-click = "close(); clear()">Send</a>\n </div>\n </div>\n
Run Code Online (Sandbox Code Playgroud)\n
您确定正确初始化 bootstrap js 吗?
我做了一个简单的代码笔:http://codepen.io/dschu/pen/LpYdZY ?editors=101
脚本语言
$('.modal').on('hidden.bs.modal', function () {
console.log('IT IS HIDDEN!')
});
Run Code Online (Sandbox Code Playgroud)
检查你的控制台。“它被隐藏了!” 模式关闭后将立即显示。