【发布时间】:2015-02-03 15:12:24
【问题描述】:
我正在开发一个简单的 Ionic 移动应用程序,尽管答案可能在于 Angular。该应用程序非常简单,显示带有添加按钮的员工列表,该按钮显示模式,让用户输入一些详细信息,单击“保存”并将数据保存到后端 Firebase 商店。它有 1 个控制器和一个简单的服务。最初,我在 index.html 中有用于模态脚本标签的模板 html,并且一切正常。当我决定构建结构并将模态模板放在一个单独的 html 文件中时,突然间,通过输入框分配给 ng-modal 的数据对象不再将任何数据传递给事件处理程序以保存数据,而是始终未定义。其他一切正常,模态显示正常,事件处理程序正在调用正确的函数等。唯一的变化是将输入模板移动到单独的文件中。我知道这很可能是一件非常简单的事情,但我一生都无法弄清楚原因,也无法在其他任何地方找到有关它的任何信息。
模态的模板 HTML 文件:
<ion-list>
<h1>Add Employee</h1>
<div class="list list-inset">
<ion-item>
<label class="item item-input">
<input type="text" placeholder="Employee Name" ng-model="data.employeeName">
</label>
<label class="item item-input">
<input type="text" placeholder="Employee Age" ng-model="data.employeeAge">
</label>
</ion-item>
<button class="button button-outline button-block button-balanced"
ng-click="addEmployee(true, data)">
Save & Add Another
</button>
<button class="button button-outline button-block button-positive"
ng-click="addEmployee(false, data)">
Save
</button>
<button class="button button-outline button-block button-assertive"
ng-click="closeAddModal()">
Cancel
</button>
</ion-list>
</ion-modal-view>
addEmployee 事件 - 数据参数现在始终未定义。与嵌入式模板一起工作正常:
$scope.addEmployee = function(retainModal, data) {
var employee = {employeeName:data.employeeName,
employeeAge:data.employeeAge};
employeeService.saveEmployee(employee);
if (! retainModal) {
$scope.closeAddModal();
};
data.employeeName = "";
data.employeeAge = "";
};
【问题讨论】:
-
员工服务在哪里?你在兑现承诺吗?
-
不返回承诺。 addEmployee 函数没有调用employeeService.saveEmployee,因为它在尝试组装员工对象时出错,因为从模板传回时数据参数未定义。当模态模板 html 在 index.html 中时,这工作得很好,但是一旦我将它移到它自己的文件中,我就会遇到这个问题。