我正在關注Django和Angular開發中的Pluralsight課程。我非常新到兩個框架,但與其他語言和框架方面的經驗。在指令中處理http動詞的最佳方法
在本課程中,我們將展示如何使用debounce
作爲選項,並在ng-change
期間調用函數。此功能update
使用http.put
發送就把請求到本地REST API。
卻有着與以往語言工作使用ng-change
將請求發送到Web服務(甚至debounce
設置爲500)似乎是一個非常愚蠢的想法,因爲會有很多不必要的流量發送到的念頭服務。
怎麼可能做得更好?我試圖在按鈕上設置ng-click
來觸發update
,但由於某種原因它似乎沒有觸發事件...
代碼中沒有去抖動,因爲我根本就沒有知道我是否應該使用這種方法..
編輯:
正如在其中一個答案已經提到它可能是使用ng-keyup
一個好主意,如果我在做某種在控制的搜索與自動更正/填充,但在這種情況下,我擔心提交(更新)數據庫中的數據與put
請求你唱歌ng-keyup
或ng-change
會造成太多開銷流量。
card.html
<div class="card" ng-hide="edit" ng-click="edit=true">
<h4> {{ card.title }} </h4>
<span class="description"> {{ card.description }} </span>
</div>
<div class="card" ng-show="edit">
<div class="flex">
<label><strong>Title: </strong></label>
<input type="text" ng-model="card.title"
ng-change="update()" />
</div>
<textarea ng-model="card.description"
ng-change="update()"
placeholder="Enter description here..."></textarea>
<button ng-click="edit=false">Close</button>
</div>
card.directive.js:
(function() {
"use strict";
angular.module('scrumboard.demo')
.directive('scrumboardCard', CardDirective);
function CardDirective() {
return {
templateUrl: "/static/scrumboard/card.html",
restrict: 'E',
controller: ['$scope', '$http', function ($scope, $http) {
let url = '/scrumboard/cards/' + $scope.card.id + '/';
console.log("Inside");
$scope.update = function() {
console.log("I'm inside");
$http.put(url, $scope.card)
.finally(function() {
edit = false;
});
};
}]
};
}
})();
爲什麼不放置搜索圖標並使用'ng-click'來打電話。 – Ved
@Ved 是的,這也是我的想法,但是當我在按鈕中添加'ng-click =「update()」'時,它不會觸發更新。我試着在幾個地方添加'console.log',但出於某種原因,好像事件並沒有被解僱。 – geostocker
你可以用同樣的問題創建一個小提琴嗎? – Ved