当用户选择一个名称
时,我尝试将id作为参数传递给搜索功能<input type="text" ng-model="RegionName" placeholder="Region" ng-blur="search(item.id)" uib-typeahead="item as item.name for item in items | filter:$viewValue | limitTo:10" class="form-control" >
我的JSON数据:
json : {"id":"000","name":"value1"},{"id":"001","name":"value2"}
Angularjs代码:
$scope.search = function (code) {
alert(code);
}
我得到的代码未定义。
我的错误在哪里。请提出你的建议。
答案 0 :(得分:0)
将Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/user-profile/edit/preferences
Django Version: 1.11.1
Python Version: 3.5.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'dating.dating',
'dating_site']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/venv/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/venv/lib/python3.5/site-packages/django/contrib/auth/mixins.py" in dispatch
56. return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
File "/venv/lib/python3.5/site-packages/django/contrib/auth/mixins.py" in dispatch
116. return super(UserPassesTestMixin, self).dispatch(request, *args, **kwargs)
File "/venv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/dating/dating/dating/views/user_profile.py" in post
99. return super(UserProfilePartnerPreferencesUpdateView, self).post(request, *args, **kwargs)
File "/venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post
240. return super(BaseUpdateView, self).post(request, *args, **kwargs)
File "/venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post
183. return self.form_valid(form)
File "/venv/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
162. self.object = form.save()
File "/venv/lib/python3.5/site-packages/django/forms/models.py" in save
452. self._save_m2m()
File "/venv/lib/python3.5/site-packages/django/forms/models.py" in _save_m2m
434. f.save_form_data(self.instance, cleaned_data[f.name])
File "/venv/lib/python3.5/site-packages/django/db/models/fields/related.py" in save_form_data
1686. getattr(instance, self.attname).set(data)
File "/venv/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py" in set
982. objs = tuple(objs)
Exception Type: TypeError at /events/dating/user-profile/edit/preferences
Exception Value: 'NoneType' object is not iterable
更改为ng-blur="search(item.id)
,它会起作用。,因为您将数据绑定到另一个属性ng-blur="search(RegionName.id)
。
此外,我发现RegionName
存在typeahead问题,因此我建议使用use ng-blur
。
查看此工作Plunker