【发布时间】:2019-06-29 10:05:45
【问题描述】:
我创建了 ng-repeat 指令来提供单选按钮列表,并尝试访问控制器中用户选择的值,但我的值未定义。这是servicenow平台的代码,请告诉我有什么问题。
<div>
<!-- your widget template -->
<div class= "panel panel-default">
<div class="panel-heading">
<h3 class="panel-title" align="center">
Select the options below to make a request:
</h3>
</div>
<body>
<div class="btn-group btn-group-justified">
<div ng-repeat="item in data.items">
<input type="radio"
name="item"
data-ng-model="$parent.item"
ng-checked="$first"
id="{{::item.name}}"
ng-value="{{::item.sys_id}}">
<label class="form-check-label" align="center">
<h5> {{::item.name}}</h5>
</label>
</div>
<div>
<input type="radio"
name="select-service"
ng-model="result"
id="service3"
ng-value="'Other'">
<label class="form-check-label" align="center">
<h5> OTHER</h5>
</label>
</div>
</div>
<div ng-if="result=='Other'" class="md-block" style="width: 600px">
<span>Select the item that you are interested</span>
<sn-record-picker id="item1"
field="location"
table="'u_sc_cat_item_ext'"
display-fields="'name,sc_catalogs'"
value-field="'sys_id'"
search-fields="'name'"
default-query="data.queryString">
</sn-record-picker>
</div>
<br>
<br>
<button type="button"
class="btn btn-primary btn-lg"
id="proceed1"
ng-click="RequestService()">Proceed</button>
</body>
</div>
</div>
控制器:
function ($scope,
$element,
$window,
$location,
$rootScope,
$timeout,
snAttachmentHandler,
$http,
spUtil,
userPreferences,
$filter,
i18n,
$uibModal) {
/* widget controller */
var c = this;
$scope.RequestService = function() {
$scope.data.action = $scope.item;-->>getting undefined value
c.data.item1 = $('#item1').select2('val');
$scope.server.update().then(function() {
$scope.data.action = undefined;
$scope.data.message = "";
if ($scope.result == undefined) {
$window.alert("RadioButton not checked.");
//window.location.href='?id=hhs_request_page';
}
else {
window.location.href = $scope.data.href;
}
});
};
}
【问题讨论】:
-
$parent的使用是code smell,这是更深层次问题的征兆。
标签: angularjs