<form class="form-horizontal" role="form" ng-submit="cal.addEvent()"novalidate>
<div class="form-group">
<label class="control-label col-sm-2" for="title">Title:</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="title" placeholder="Event Title" ng-model="cal.newEvent.title">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="desc">Description:</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="desc" placeholder="Event Description" ng-model="cal.newEvent.description">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="stime">Start Time:</label>
<div class="col-sm-8">
<input type="time" class="form-control" id="stime" ng-model="cal.newEvent.start">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="etime">End Time:</label>
<div class="col-sm-8">
<input type="time" class="form-control" id="etime" ng-model="cal.newEvent.end">
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<button type="submit" class="btn btn-success btn-block" data-role="none">Add Event</button>
</div>
</div>
</form>
此表单位于指令中,如下所示:
app.directive("calendar", function($http) {
return {
restrict: "E",
templateUrl: "templates/calendar.html",
scope: true,
transclude:true,
link: function(scope) {
//there's a bunch of code here that I don't believe has anything to do with ng-submit so i left it out
},
controller: ["$scope", "$rootScope", function($scope, $rootScope){
this.newEvent = {};
this.removeEvent = function(index){
$http.post("classes/main.php", {"fn": "calendarDel", "id": $scope.chosen[index].id}).success(function(data){
$scope.getEvents($scope.chosen[index].date);
});
}
this.addEvent = function(){
//this.newEvent.date = $scope.dateString;
console.log("AddEvent");
console.log(this.newEvent);
}
$scope.getEvents = function(date){
$http.post("classes/main.php", {"fn": "calendar", "id": $rootScope.user.id, "data": date}).success(function(data){
if(!data.Error){
$scope.chosen = data;
}
});
}
}],
controllerAs: 'cal'
};
});
问题是,当我尝试提交表单时,我没有看到该函数被调用的迹象......我希望至少看到console.log("AddEvent");
有人在这里看到可能导致此问题的原因吗?
FYI
表单位于bootstrap 3模态div中,它位于调用它的同一个指令内---如果你需要看到“更大的图片”,可以这么说,只要问:)
我已尝试this.addEvent()
,$scope.addEvent()
,$rootScope.addEvent()
无变更
答案 0 :(得分:1)
您应该致电addEvent()
<form class="form-horizontal" role="form" ng-submit="addEvent()" novalidate>
并将函数绑定到范围,如
$scope.addEvent = function() {
上的角度文档
表达式是根据范围对象
进行评估的
所以你不应该把表达式写成ng-submit="$scope.addEvent()"
等
答案 1 :(得分:1)
您应该向指令添加bindToController: true
并指定scope: {}
以创建隔离范围。
当使用隔离范围作为组件(参见上文)并使用controllerAs时,bindToController:true将允许组件将其属性绑定到控制器,而不是范围。实例化控制器时,隔离范围绑定的初始值已经可用。
参考:$compile