我怎麼能在這裏優化的角度重複代碼
angular.module('myApp')
.controller('LogsController', function ($scope, LogsService) {
$scope.updatingLogs = true;
$scope.loggers = {};
LogsService.findAll().$promise.then(function(data) {
$scope.loggers = data;
$scope.updatingLogs = false;
});
$scope.changeLevel = function (name, level) {
LogsService.changeLevel({name: name, level: level}, function() {
$scope.updatingLogs = true;
LogsService.findAll().$promise.then(function(data) {
$scope.loggers = data;
$scope.updatingLogs = false;
});
});
};
});
這真的很接近我正要說的話。區別在於findAll()不在$ scope中,除非視圖需要直接調用它。 – coblr
@fractalspawn ya..i在$ scope中添加了它,因爲無論哪種方式,我們都可以使用'ng-init'來初始化它,這會使得單元測試更有意義 –
如果它確實幫助了你,它就會變成upvote ..謝謝:) –