gpt4 book ai didi

javascript - Angular $scope.list[index] 不更新

转载 作者:行者123 更新时间:2023-12-03 05:10:19 26 4
gpt4 key购买 nike

我遇到的问题是,当我更新一个项目时, $scope.list 中的对象数组会更新,但是当我想要使用 $scope.list[index] 来获取特定项目时,它不会更新。以下日志给了我这个输出。

console.log("HEAD");
console.log($scope.list);
console.log(index);
console.log(index + ".id: " +$scope.list[index].id);
console.log(index + ".joke: " +$scope.list[index].joke);
console.log($scope.list);
console.log("END");

当我在浏览器中进行更改并希望将它们推回到后端时,就会发生这种情况。

控制台是:

enter image description here

此外,当我打开特定项目的日志时,它会发生变化。

enter image description here

Controller

(function () {
'use strict';

angular.module('BlurAdmin.pages.entertainment')
.config(['$httpProvider', function ($httpProvider) {
$httpProvider.defaults.headers.common['Authorization'] = 'Basic MTpwYXNzd29yZA==';
delete $httpProvider.defaults.headers.common["X-Requested-With"];
}])
.factory('myService', function ($http) {
var getData = function () {

// Angular $http() and then() both return promises themselves
return $http.get('http://192.168.12.11:8090/joke').then(function (result) {

// What we return here is the data that will be accessible
// to us after the promise resolves
return result.data;
});
};

var putData = function (joke, index) {
console.log("PUT");
console.log(joke[0]);
return $http.put('http://192.168.12.11:8090/joke/' + joke[index].id, joke[index]);
};
var remData = function (index) {
console.log("REMOVE");
return $http.delete('http://192.168.12.11:8090/joke/' + index);
};
return {
getData: getData,
putData: putData,
remData: remData
};
})

.controller('EntertainmentCtrl', EntertainmentCtrl);

/** @ngInject */
function EntertainmentCtrl($scope, $timeout, editableOptions, editableThemes, myService, $filter) {
$scope.progressFunction = function () {
return $timeout(function () { }, 3000);
};

var myDataPromise = myService.getData();
$scope.selectedFromList = null;
myDataPromise.then(function (result) {
console.log("promise");
console.log(result);
$scope.list = result;
});

$scope.removeJoke = function (index) {
$scope.list.splice(index, 1);
myService.remData($scope.list[index].id);
};

$scope.addJoke = function () {
$scope.inserted = {
id: $scope.list.length + 1,
joke: '',
};
myService.putData($scope.inserted);
$scope.list.push($scope.inserted);
};

$scope.save = function (index) {
console.log("HEAD");
console.log($scope.list);
console.log(index);
console.log(index + ".id: " + $scope.list[index].id);
console.log(index + ".joke: " + $scope.list[index].joke);
console.log($scope.list);
console.log("END");
$scope.inserted = {
id: $scope.list[index].id,
joke: $scope.list[index].joke,
};

var help = $scope.list;
console.log("var help");
console.log(help[index]);
myService.putData(help, index);
}

editableOptions.theme = 'bs3';
editableThemes['bs3'].submitTpl = '<button type="submit" class="btn btn- primary btn-with-icon"><i class="ion-checkmark-round"></i></button>';
editableThemes['bs3'].cancelTpl = '<button type="button" ng-click="$form.$cancel()" class="btn btn-default btn-with-icon"><i class="ion-close-round"></i></button>';

}
})();

景色

<div class="add-row-editable-table">
<button class="btn btn-primary" ng-click="addJoke()">Grap toevoegen</button>
</div>
<table class="table table-bordered table-hover table-condensed">
<tr>
<td>Grap</td>
</tr>
<tr ng-repeat="item in list" class="editable-row">
<td>
<span editable-text="item.joke" e-name="joke" e-form="rowform" e-required>
{{ item.joke || 'empty' }}
</span>
</td>
<td>
<form editable-form name="rowform" ng-show="rowform.$visible" class="form-buttons form-inline" shown="inserted == list">
<button type="submit" ng-disabled="rowform.$waiting" class="btn btn-primary editable-table-button btn-xs" ng-click="save($index)">
Save
</button>
<button type="button" ng-disabled="rowform.$waiting" ng-click="rowform.$cancel()" class="btn btn-default editable-table-button btn-xs">
Cancel
</button>
</form>
<div class="buttons" ng-show="!rowform.$visible">
<button class="btn btn-primary editable-table-button btn-xs" ng-click="rowform.$show()">Edit</button>
<button class="btn btn-danger editable-table-button btn-xs" ng-click="removeJoke($index)">Delete</button>
</div>
</td>
</tr>
</table>

最佳答案

尝试如下。

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script type="text/javascript">
var app= angular.module("myApp",[]);

app.controller("myCTRL",function($scope){

$scope.names = [{name: 'arun'},{name: 'Mozhi'}];
});
</script>

<body ng-app="myApp" ng-controller="myCTRL">
<div>
</div><br>
<div ng-repeat="item in names">
<form novalidate>
<input type="text" placeholder="name" ng-model="item.name">

<br><br>
</form>

</div>
{{names}}
</body>

关于javascript - Angular $scope.list[index] 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41851985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com