我試圖從表中的API中排序一些數據。我知道,當我在控制器中擁有所有這些功能時,這些功能都可以正常工作,但現在我正在將這種擔憂分解爲一條指令,因爲我相信我應該這樣做,而這正是我遇到麻煩的地方。查看不從指令功能更新
通過一系列的console.log
的我能夠看到所有的功能似乎正在工作,並且當單擊標題時,新的數據集將按照我的預期返回。一旦表頭被點擊,它只是不更新刷新數據的視圖。
我試過擺弄apply()
和watch()
,但沒有任何工作對我來說。
這裏是我的指令的下調版本:
angular.module('myApp')
.directive('sortOrder', [
'$routeParams',
'GetData',
'SortData',
function($routeParams, GetData, SortData) {
return {
restrict: 'A',
controller: function() {
console.log("Sortable");
// Set this to a variable self to avoid closure issues
var self = this;
// Set the module declared endpoint to a variable
self.endpoint = $routeParams.endpoint;
// Initially set the column sorting to Ascending
self.sortOrder = "ASC";
self.setOrder = function (column) {
console.log("Setting order");
GetData.getData(self.endpoint, "&orderBy[" + column + "]=" + self.sortOrder).then(function(data){
console.log("Fetching");
self.presentData = SortData.sortData(self.endpoint, data);
console.log(data);
});
});
},
controllerAs: 'sort'
};
}]);
我得到的Sortable
,Setting order
,Fetching
&數據對象都正確console.log
的。
正如你可以看到它在2個服務要求獲得數據和提交數據,他們有點過於龐大這裏要投入,但這裏的地方被點擊的標題我的HTML的一部分:
<thead data-sort-order>
<tr>
<th ng-repeat="header in display.headers" class="{{ header }}" ng-click="sort.setOrder(header)"><span>{{ header }}</span></th>
</tr>
</thead>
我希望這會有道理。讓我知道是否還有其他可能需要的東西。
非常感謝提前!
編輯=====
下面是一些HTML的呈現數據的請求被刷新:
<tr ng-repeat="entries in display.presentData" class="table-row">
<!-- Using (key, details) also allows access to the key of each object value -->
<td ng-repeat="(key,details) in entries track by $index" class="{{ key }}">{{ details }}</td>
</tr>
嘗試使用'sort.presentData'而不是'display.presentData'。我不確定你的HTML的位置,但是你需要改變變量名或者在你的控制器的presentData和你的指令的presentData之間建立一個雙向綁定,在第二種情況下它需要一個更復雜的答案 – valepu
缺少一些東西:要麼在指令中指定一個模板。或者使用作用域繼承爲了注入像ng-repeat這樣的新變量,用$ index – Walfrat
感謝@valepu,但是改成'sort.presentData'並沒有解決這個問題。所以我認爲這可能是更復雜的第二選擇。 –