【问题标题】:ng-submit won't call function inside directive controllerng-submit 不会在指令控制器中调用函数
【发布时间】:2015-10-21 04:10:11
【问题描述】:

好的,我认为问题说明了一切,但为了清楚起见,我有以下表格(我知道它很长......我正在使用引导程序......和 ​​jquery):

<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");

有人知道是什么原因导致了这个问题吗?


仅供参考

该表单位于 bootstrap 3 modal div 中,它位于调用它的同一指令中 --- 如果您需要查看“更大的图片”,可以这么说,请问:)

我试过this.addEvent(),$scope.addEvent(),$rootScope.addEvent()没有变化

【问题讨论】:

    标签: javascript angularjs forms twitter-bootstrap


    【解决方案1】:

    你应该打电话给addEvent()

    <form class="form-horizontal" role="form" ng-submit="addEvent()" novalidate>
    

    并将函数绑定到您的范围,例如

    $scope.addEvent = function() {
    

    来自expresssions 上的角度文档

    表达式是根据作用域对象计算的

    所以你不应该把表达式写成ng-submit="$scope.addEvent()"等

    【讨论】:

    • @JamieMcAllister 它适用于plunker。也许你可以创建一个 plunker 让我们看看
    【解决方案2】:

    您应该将bindToController: true 添加到指令中并指定scope: {} 以创建隔离范围。

    当隔离范围用于组件(见上文),并且使用了 controllerAs 时,bindToController: true 将允许组件将其属性绑定到控制器,而不是绑定到范围。实例化控制器时,隔离作用域绑定的初始值已经可用。

    参考:$compile

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多