您可以提前在控制器中的 $scope 对象上定义对象(在您的示例中,我们谈论的是 myObject),然后只需使用 ng-model 将字段绑定到目的。不过,您不必提前定义对象。 ngModel 会在绑定时为你创建对象,如果可以的话。
<form name="frm" ng-submit="myControllerFunc(myObject)">
From date:
<input type="date" ng-model="myObject.fromDate" />
<br />To date:
<input type="date" ng-model="myObject.toDate" />
<br />
<button type="submit">Submit</button>
<small>Check console log for value upon submit</small>
</form>
对于函数,在控制器中的$scope 对象上定义它。在此示例中,传入对象是多余的,因为您可以在 $scope 而不是参数上引用它,但您可能有自己的理由。
$scope.myControllerFunc = function(obj) {
console.log(obj); // do whatever you want with obj
};
Demo.
为了完整起见,如果您提前定义,这里是控制器的主体。
$scope.myObject = { fromDate: null, toDate: null };
$scope.myControllerFunc = function() {
console.log($scope.myObject); // do whatever you want with obj
};
除了您可以从ng-submit 中删除参数外,视图不会有任何不同:
<form name="frm" ng-submit="myControllerFunc()">
根据您的需要混合搭配。
此外,您不必使用ng-submit 来调用我的示例中所示的函数(我没有看到您的编辑包含此详细信息)。您可以像在 ng-submit 中使用它一样,从您的按钮 ng-click 调用 myControllerFunc。