问题描述
<script src="js/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
刷新页面后直接更新存储的数据时,在控制台中出现诸如TypeError的错误:无法在Object.vm.edit(controller.js:70)上设置未定义的属性"myid",表为不会以这种方式进行更新,但是如果先添加一行然后进行更新,它将得到更新.谁能帮我解决问题?我的朋克
When I am updating the stored data directly after refreshing the page, I am getting some error in console like TypeError: Cannot set property 'myid' of undefined at Object.vm.edit (controller.js:70), table is not updating in that way, but it gets updated if you add a row first and then update it. Can anyone help me how to fix it ? My plunk
推荐答案
您的错误表明控制器上不存在数据,因此您只需要在控制器顶部初始化vm.data:
Your error says that data doesn't exist on controller, so you just need to initialize vm.data at the top of your controller:
vm.data = {};
这里是plnkr: http://plnkr.co/edit/mnRGSmEk0q9N2dSazEHL?p=preview
另一种解决方案是在添加数据对象时创建它:
Another solution would be to create data object when you're adding it:
vm.data = {
myid: id,
fname: vm.arr[i].fname,
lname: vm.arr[i].lname,
classs: vm.arr[i].classs,
age: vm.arr[i].age,
fees: vm.arr[i].fees,
};
这里是plnkr: http://plnkr.co/edit/TZmgPz2h02peBYaF0uyV?p=preview
这篇关于在刷新AngularJS中的页面后更新Crud表时,键入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!