我想創建一個功能類似於SO的「可能已經有你的答案的問題」功能。也就是說,當我的表單模型更新時,我想發送一個HTTP請求到我的後端,在提交表單並創建一個新模型之前,搜索數據庫中已有的任何類似模型。
下面是一個簡單的表格:
<div ng-controller="ExampleController">
<form>
Name:
<input type="text" ng-model="resource.name" ng-model-options="{ updateOn: 'blur' }"><br>
Other data:
<input type="text" ng-model="resource.data" ng-model-options="{ updateOn: 'blur' }"><br>
</form>
<div>
<p>Related resources</p>
<ul>
<li ng-repeat="resource in relatedResources">{{resource.name}}</li>
</ul>
</div>
</div>
而我的JS代碼:
angular.module('myApp', [])
.controller('ExampleController', ['$scope', '$http', function($scope, $http) {
$scope.resource = {};
$scope.$watchCollection('resource', function() {
$http.post(apiUrl, resource)
.success(function(data) {
$scope.relatedResources = data.resources;
});
});
}]);
$watchCollection
似乎忽視了updateOn: 'blur'
選項。有沒有辦法確保只有在模型實際更新時才發送HTTP請求?
絕對正確的1.3版本...假設你正在使用那一個 – beauXjames 2014-10-01 20:05:09
我更新到v1.3,現在一切似乎正常工作!我想我正在使用1.2。謝謝! – chipit24 2014-10-01 20:11:31