anugularJS를 사용하여 n 수준의 계층 적 순서가 지정되지 않은 목록을 생성하려고 시도 했으므로 성공적으로 수행 할 수있었습니다. 하지만 이제 지시문과 컨트롤러 사이에 범위 문제가 있습니다. 지시문 템플릿에서 ng-click이라는 함수 내에서 부모의 scope 속성을 변경해야합니다.angularJS를 사용하여 지시문과 컨트롤러 사이의 범위를 올바르게 바인딩하는 방법
는 http://jsfiddle.net/ahonaker/ADukg/2046/을 참조하십시오 - 여기 JS
var app = angular.module('myApp', []);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
$scope.itemselected = "None";
$scope.organizations = {
"_id": "SEC Power Generation",
"Entity": "OPUNITS",
"EntityIDAttribute": "OPUNIT_SEQ_ID",
"EntityID": 2,
"descendants": ["Eastern Conf Business Unit", "Western Conf Business Unit", "Atlanta", "Sewanee"],
children: [{
"_id": "Eastern Conf Business Unit",
"Entity": "",
"EntityIDAttribute": "",
"EntityID": null,
"parent": "SEC Power Generation",
"descendants": ["Lexington", "Columbia", "Knoxville", "Nashville"],
children: [{
"_id": "Lexington",
"Entity": "OPUNITS",
"EntityIDAttribute": "OPUNIT_SEQ_ID",
"EntityID": 10,
"parent": "Eastern Conf Business Unit"
}, {
"_id": "Columbia",
"Entity": "OPUNITS",
"EntityIDAttribute": "OPUNIT_SEQ_ID",
"EntityID": 12,
"parent": "Eastern Conf Business Unit"
}, {
"_id": "Knoxville",
"Entity": "OPUNITS",
"EntityIDAttribute": "OPUNIT_SEQ_ID",
"EntityID": 14,
"parent": "Eastern Conf Business Unit"
}, {
"_id": "Nashville",
"Entity": "OPUNITS",
"EntityIDAttribute": "OPUNIT_SEQ_ID",
"EntityID": 4,
"parent": "Eastern Conf Business Unit"
}]
}]
};
$scope.itemSelect = function (ID) {
$scope.itemselected = ID;
}
}
app.directive('navtree', function() {
return {
template: '<ul><navtree-node ng-repeat="item in items" item="item" itemselected="itemselected"></navtree-node></ul>',
restrict: 'E',
replace: true,
scope: {
items: '='
}
};
});
app.directive('navtreeNode', function ($compile) {
return {
restrict: 'E',
template: '<li><a ng-click="itemSelect(item._id)">{{item._id}} - {{itemselected}}</a></li>',
scope: {
item: "=",
itemselected: '='
},
controller: 'MyCtrl',
link: function (scope, elm, attrs) {
if ((angular.isDefined(scope.item.children)) && (scope.item.children.length > 0)) {
var children = $compile('<navtree items="item.children"></navtree>')(scope);
elm.append(children);
}
}
};
});
그리고 여기에 HTML 목록은 모델에서 생성
<div ng-controller="MyCtrl">
Selected: {{itemselected}}
<navtree items="organizations.children"></navtree>
</div>
주입니다. ng-click은 부모 범위 속성 (itemselected)을 설정하는 함수를 호출하지만 변경은 로컬에서만 발생합니다. 예상되는 동작은 항목을 클릭 할 때 "Selected : None"이 "Selected : xxx"로 변경되어야한다는 것입니다. 여기서 xxx는 클릭 된 항목입니다.
부모 범위와 지시문 사이의 속성을 적절하게 바인딩하지 않습니까? 부모 범위에 속성 변경을 전달하려면 어떻게해야합니까?
희망이 있습니다.
미리 도움을 청하십시오.
Welcome to StackOverflow! 끝 부분의 실제 질문 형태로 다시 말하면 게시물이 향상 될 수 있습니다. –