각도 UI 모달의 범위 문제
각도 UI 모달의 스코프를 이해하거나 사용하는 데 문제가 있습니다.
여기에서는 금방 알 수 없지만 모듈이나 모든 것을 올바르게 셋업하고 있습니다만, 특히 이 코드 샘플이 버그를 발견하는 장소입니다.
index.module(중요한 부분)
<div class="btn-group">
<button class="btn dropdown-toggle btn-mini" data-toggle="dropdown">
Actions
<span class="caret"></span>
</button>
<ul class="dropdown-menu pull-right text-left">
<li><a ng-click="addSimpleGroup()">Add Simple</a></li>
<li><a ng-click="open()">Add Custom</a></li>
<li class="divider"></li>
<li><a ng-click="doBulkDelete()">Remove Selected</a></li>
</ul>
</div>
Controller.js(중요한 부분)
MyApp.controller('AppListCtrl', function($scope, $modal){
$scope.name = 'New Name';
$scope.groupType = 'New Type';
$scope.open = function(){
var modalInstance = $modal.open({
templateUrl: 'partials/create.html',
controller: 'AppCreateCtrl'
});
modalInstance.result.then(function(response){
// outputs an object {name: 'Custom Name', groupType: 'Custom Type'}
// despite the user entering customized values
console.log('response', response);
// outputs "New Name", which is fine, makes sense to me.
console.log('name', $scope.name);
});
};
});
MyApp.controller('AppCreateCtrl', function($scope, $modalInstance){
$scope.name = 'Custom Name';
$scope.groupType = 'Custom Type';
$scope.ok = function(){
// outputs 'Custom Name' despite user entering "TEST 1"
console.log('create name', $scope.name);
// outputs 'Custom Type' despite user entering "TEST 2"
console.log('create type', $scope.groupType);
// outputs the $scope for AppCreateCtrl but name and groupType
// still show as "Custom Name" and "Custom Type"
// $scope.$id is "007"
console.log('scope', $scope);
// outputs what looks like the scope, but in this object the
// values for name and groupType are "TEST 1" and "TEST 2" as expected.
// this.$id is set to "009" so this != $scope
console.log('this', this);
// based on what modalInstance.result.then() is saying,
// the values that are in this object are the original $scope ones
// not the ones the user has just entered in the UI. no data binding?
$modalInstance.close({
name: $scope.name,
groupType: $scope.groupType
});
};
});
create.create(전체)
<div class="modal-header">
<button type="button" class="close" ng-click="cancel()">x</button>
<h3 id="myModalLabel">Add Template Group</h3>
</div>
<div class="modal-body">
<form>
<fieldset>
<label for="name">Group Name:</label>
<input type="text" name="name" ng-model="name" />
<label for="groupType">Group Type:</label>
<input type="text" name="groupType" ng-model="groupType" />
</fieldset>
</form>
</div>
<div class="modal-footer">
<button class="btn" ng-click="cancel()">Cancel</button>
<button class="btn btn-primary" ng-click="ok()">Add</button>
</div>
여기서 궁금한 점은 스코프가 UI에 더블바인드 되어 있지 않은 이유와this커스터마이즈된 가치를 가지지만$scope그렇지 않나요?
추가하려고 했습니다.ng-controller="AppCreateCtrl"create.html의 body div에 도달했지만, 그것이 오류를 던졌다: "Unknown provider: $modal.Instance Provider <- $modal'인스턴스'는 운이 없다.
현시점에서 유일한 선택사항은 오브젝트를 반환하는 것입니다.this.name그리고.this.groupType사용하는 대신$scope하지만 그건 잘못된 느낌이야
중첩된 범위가 포함된 경우 바인딩하지 마십시오.<input>s는 스코프의 멤버에게 직접 송신됩니다.
<input ng-model="name" /> <!-- NO -->
이들을 적어도 더 깊은 레벨로 바인드합니다.
<input ng-model="form.name" /> <!-- YES -->
그 이유는 스코프가 원형적으로 상위 스코프를 상속하기 때문입니다.따라서 첫 번째 수준 구성원을 설정할 때 부모에 영향을 주지 않고 자식 범위에서 직접 설정됩니다.이와는 대조적으로 중첩된 필드에 바인딩하는 경우(form.name)멤버form부모 스코프에서 읽기 때문에name속성이 올바른 대상에 액세스합니다.
자세한 내용은 여기를 참조하십시오.
내 일은 이렇게 하는 거야
var modalInstance = $modal.open({
templateUrl: 'partials/create.html',
controller: 'AppCreateCtrl',
scope: $scope // <-- I added this
});
양식 이름 없음, 없음$parent. Angular를 사용하고 있습니다.UI 부트스트랩 버전 0.12.1.
나는 이것에 의해 이 해결책에 대한 힌트를 얻었다.
2014년 11월 갱신:
실제로 코드는 ui-bootstrap 0.12.0으로 업그레이드한 후에 작동합니다.변환된 범위가 컨트롤러의 범위와 병합되므로 더 이상 필요하지 않습니다.$parent또는form.물건.
0.12.0 이전 버전:
모달은 트랜슬루션을 사용하여 내용을 삽입합니다.덕분에.ngForm스코프를 제어할 수 있습니다.name기여하다.따라서 변환된 범위를 벗어나려면 다음과 같이 양식을 수정하십시오.
<form name="$parent">
또는
<form name="$parent.myFormData">
모델 데이터는 컨트롤러 범위에서 사용할 수 있습니다.
$scope.open = function () {
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'salespersonReportController',
//size: size
scope: $scope
});
};
내 범위: $scope thank u Jason Swett
scope:$scope를 추가하면 동작합니다.멋져
언급URL : https://stackoverflow.com/questions/18924577/scope-issues-with-angular-ui-modal
'codememo' 카테고리의 다른 글
| Sublime Text 3에서 TypeScript를 지원하는 방법 (0) | 2023.03.29 |
|---|---|
| 폼데이터를 사용한 JavaScript Blob 업로드 (0) | 2023.03.19 |
| 일부 테스트에서 페이지 전체를 새로고침했습니다.Jasmine 테스트 실행 중 오류가 발생했습니다. (0) | 2023.03.19 |
| MongoDB 클러스터란? (0) | 2023.03.19 |
| Facebook SDK가 오류를 반환했습니다. 교차 사이트 요청 위조 유효성 검사에 실패했습니다.URL과 세션의 "상태" 매개 변수가 일치하지 않습니다. (0) | 2023.03.19 |