AngularJS编辑功能前端视图值无法更新,求技术帮助
Hey there! Let's figure out why your edit functionality's view values aren't updating as expected. Looking at your code snippets, there are a couple of key issues that are likely causing this problem—let's break them down and fix them step by step.
Key Problems in Your Current Code
Missing Two-Way Data Binding
Your<textarea>is using{{edit.text}}which only does one-way rendering. That means when you type in the textarea, the underlyingedit.textvalue won't update, and vice versa if you changeedit.textin your controller, the view might not reflect it properly.Unclear Association to the Current Book
YoureditPost()method isn't receiving the specificbookfrom theng-repeatloop, so you're not initializing the edit state with the correct book data. Plus, ifeditisn't a reference-type object, you might hit AngularJS scope inheritance issues withng-repeat's child scopes.Redundant Condition Checks
You're using bothng-show="showEditPost"on the parent div andng-if="showEditPost == true/false"on the child elements—this isn't breaking things, but it's unnecessary and can make the code harder to follow.
Updated Code Solution
Step 1: Modify the HTML Template
First, update your markup to pass the current book to the edit function, add proper two-way binding to the textarea, and clean up the conditionals:
<li ng-click="delegate.editPost(book)">Edit</li> <div ng-show="delegate.showEditPost"> <div ng-click="delegate.savePost()">Save</div> <!-- Show original text when not editing --> <p ng-if="!delegate.showEditPost">{{book.text}}</p> <!-- Edit mode with two-way binding --> <div ng-if="delegate.showEditPost"> <textarea ng-model="delegate.edit.text"></textarea> </div> </div>
Step 2: Update Your Controller Logic
Make sure your editPost and savePost methods correctly handle the book data and avoid scope issues:
// Assuming your controller uses controllerAs syntax with 'delegate' as the alias angular.module('yourApp').controller('BookController', function() { var vm = this; vm.books = [/* your book data here */]; vm.showEditPost = false; vm.edit = {}; // Initialize as an object to avoid scope inheritance problems // Initialize edit state with the selected book vm.editPost = function(book) { // Use angular.copy to create a deep copy—avoids modifying the original book until save vm.edit = angular.copy(book); vm.showEditPost = true; }; // Save the edited book back to the array vm.savePost = function() { // Find the index of the original book in the array var bookIndex = vm.books.findIndex(function(b) { return b.id === vm.edit.id; // Match using your book's unique identifier }); if (bookIndex !== -1) { // Update the original book with the edited data vm.books[bookIndex] = angular.copy(vm.edit); } // Exit edit mode vm.showEditPost = false; }; });
Why This Works
ng-model="delegate.edit.text": Enables two-way data binding, so changes in the textarea immediately updatevm.edit.text, and any changes tovm.edit.textin the controller show up in the view.- Passing
booktoeditPost(): Ensures you're editing the exact book from theng-repeatloop, not a generic or uninitialized object. angular.copy(): Creates a separate copy of the book, so you don't modify the original data in the array until you explicitly click "Save"—this prevents accidental unsaved changes.- ControllerAs Syntax: Using
delegate.(orvm.) prefixes avoids scope inheritance issues that can happen with primitive values inng-repeat's child scopes.
Give these changes a try, and your edit view should start updating values correctly!
内容的提问来源于stack exchange,提问作者swetha

