2017-11-17 1 views
0

지시어로 생성 된 컨트롤러 기능에서 각도 변수 값을 설정하려고합니다. 어떻게 든 그것은 어떤 알려지지 않은 이유로 작용하지 않습니다. 값은 독립적으로 설정 될 때 표시되지만 컨트롤러 기능에서 값을 할당하려고하면 작동하지 않습니다. 내 코드는 다음과 같습니다

,

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8" /> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> 

</head> 
<body> 
    <div ng-app="mainApp"> 
     <div ng-controller="MyController"> 
      <div id="Details" class="Details">{{Details}}</div></br> 
      <div id="Test" class="Test"> 
      <collection collection='testdata'></collection>    
      </div>   
     </div>    
    </div> 
</body> 
<script>  
var mainApp = angular.module("mainApp", []) 

mainApp.directive('collection', function() { 
    return { 
     restrict: "E", 
     replace: true, 
     scope: {collection: '=', showFn : '&'}, 
     template: "<ul><member ng-repeat='member in collection' member='member'></member></ul>"   
    } 
}) 

mainApp.directive('member', function ($compile) { 
    var NewChild = "<li><span ng-click=ShowDetailsFunc()>{{member.TagName}}</span></li>"; 

    return { 
     restrict: "E", 
     replace: true, 
     scope: {member: '=', ShowHideCtrlFunc : '&', ShowDetailsCtrlFunc : '&'}, 
     template: NewChild, 
     controller: 'MyController', 
     link: function (scope, element, attrs) {    
      var collectionSt = '<collection collection="member.children"></collection>'; 
      if (angular.isArray(scope.member.children)) {    
        $compile(collectionSt)(scope, function(cloned, scope) {           
         element.attr('xml-path', scope.member.TagPath); 
         element.append(cloned);       
        });      
        scope.ShowDetailsFunc = function() { 
         scope.ShowDetailsCtrlFunc(element,event);      
        } 

      } 
     } 

    } 
}) 

mainApp.controller('MyController', function ($scope) { 
    $scope.testdata = [{"TagName":"MyRootNode","TagPath":">MyRootNode","children":[{"TagName":"LandXML","TagPath":">MyRootNode>ChildItems>LandXML","children":[{"TagName":"Units","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Units","children":[{"TagName":"Imperial","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[1]>Imperial","children":[]},{"TagName":"Project","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Project","children":[]},{"TagName":"Application","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Application","children":[{"TagName":"Author","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[2]>Author","children":[]},{"TagName":"Alignments","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Alignments","children":[]},{"TagName":"Roadways","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Roadways","children":[{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[1]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[2]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[3]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[4]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[5]","children":[]}]}]}]}]},{"TagName":"Surfaces","TagPath":">MyRootNode>ChildItems>Surfaces","children":[{"TagName":"Surface1","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface1","children":[]},{"TagName":"Surface2","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface2","children":[]}]}]}] 

    $scope.Details = "defalut value" 
    $scope.ShowDetailsCtrlFunc = function(element,event) { 
      console.log("in function ShowDetailsCtrlFunc");      
      var myxmlpath = $(element).attr("xml-path") 
      $scope.Details = getObjects($scope.testdata, 'TagPath', myxmlpath)[0].TagName; 
      console.log($scope.Details)       
      //event.stopImmediatePropagation(); 
     };  
}); 
function getObjects(obj, key, val) { 
    var objects = []; 
    for (var i in obj) { 
     if (!obj.hasOwnProperty(i)) continue; 
     if (typeof obj[i] == 'object') { 
      objects = objects.concat(getObjects(obj[i], key, val)); 
     } else if (i == key && obj[key] == val) { 
      objects.push(obj); 
     } 
    } 
    return objects; 
} 
</script> 
</html> 

내가 실수를하고 위치를 이해하는 데 도움이 그것은 어떻게 수리 할 수 ​​있습니다하시기 바랍니다.

도와주세요. 미리 감사드립니다.

답변

1

순환 방식으로 지시문을 만들면 MyController -controller 중첩이 문제입니다. 결과는 $ scope.Details가 뷰의 값을 표시하는 범위 대신 특정 중첩 범위에 설정된다는 것입니다.

$로이 문제를 해결할 수 있습니다. 새 값을 업스트림으로 보내고 마지막으로 제시 범위 값에 도달하게됩니다.

여기에 예를

var mainApp = angular.module("mainApp", []) 
 

 
mainApp.directive('collection', function() { 
 
    return { 
 
     restrict: "E", 
 
     replace: true, 
 
     scope: {collection: '=', showFn : '&'}, 
 
     template: "<ul><member ng-repeat='member in collection' member='member'></member></ul>"   
 
    } 
 
}) 
 

 
mainApp.directive('member', function ($compile) { 
 
    var NewChild = "<li><span ng-click=ShowDetailsFunc()>{{member.TagName}}</span></li>"; 
 

 
    return { 
 
     restrict: "E", 
 
     replace: true, 
 
     scope: {member: '=', ShowHideCtrlFunc : '&', ShowDetailsCtrlFunc : '&'}, 
 
     template: NewChild, 
 
     controller: 'MyController', 
 
     link: function (scope, element, attrs) {    
 
      var collectionSt = '<collection collection="member.children"></collection>'; 
 
      if (angular.isArray(scope.member.children)) {    
 
        $compile(collectionSt)(scope, function(cloned, scope) {           
 
         element.attr('xml-path', scope.member.TagPath); 
 
         element.append(cloned);       
 
        });      
 
        scope.ShowDetailsFunc = function() { 
 
         scope.ShowDetailsCtrlFunc(element);      
 
        } 
 

 
      } 
 
     } 
 

 
    } 
 
}) 
 

 
mainApp.controller('MyController', function ($scope) { 
 
    $scope.testdata = [{"TagName":"MyRootNode","TagPath":">MyRootNode","children":[{"TagName":"LandXML","TagPath":">MyRootNode>ChildItems>LandXML","children":[{"TagName":"Units","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Units","children":[{"TagName":"Imperial","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[1]>Imperial","children":[]},{"TagName":"Project","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Project","children":[]},{"TagName":"Application","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Application","children":[{"TagName":"Author","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[2]>Author","children":[]},{"TagName":"Alignments","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Alignments","children":[]},{"TagName":"Roadways","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Roadways","children":[{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[1]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[2]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[3]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[4]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[5]","children":[]}]}]}]}]},{"TagName":"Surfaces","TagPath":">MyRootNode>ChildItems>Surfaces","children":[{"TagName":"Surface1","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface1","children":[]},{"TagName":"Surface2","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface2","children":[]}]}]}] 
 

 
    $scope.Details = "defalut value"; 
 
    $scope.ShowDetailsCtrlFunc = function(element) { 
 
      console.log("in function ShowDetailsCtrlFunc");      
 
      var myxmlpath = angular.element(element).attr("xml-path") 
 
      var detail = getObjects($scope.testdata, 'TagPath', myxmlpath)[0].TagName; 
 
      console.log(detail); 
 
      $scope.$emit('detailSelected',detail); 
 
      //event.stopImmediatePropagation(); 
 
     }; 
 
     $scope.$on('detailSelected',function($event, message){ 
 
     $scope.Details = message; 
 
    }); 
 
}); 
 
function getObjects(obj, key, val) { 
 
    var objects = []; 
 
    for (var i in obj) { 
 
     if (!obj.hasOwnProperty(i)) continue; 
 
     if (typeof obj[i] == 'object') { 
 
      objects = objects.concat(getObjects(obj[i], key, val)); 
 
     } else if (i == key && obj[key] == val) { 
 
      objects.push(obj); 
 
     } 
 
    } 
 
    return objects; 
 
}
<!doctype html> 
 
<html> 
 
<head> 
 
<meta charset="UTF-8" /> 
 
<script data-require="[email protected]" data-semver="1.6.4" src="https://code.angularjs.org/1.6.4/angular.min.js"></script> 
 

 
</head> 
 
<body> 
 
    <div ng-app="mainApp"> 
 
     <div ng-controller="MyController"> 
 
      <div id="Details" class="Details">{{Details}}</div></br> 
 
      <div id="Test" class="Test"> 
 
      <collection collection='testdata'></collection>    
 
      </div>   
 
     </div>    
 
    </div> 
 
</body> 
 
    
 
</html>

+1

덕분에 많은 @Marcus을합니다. 그것은 작동합니다. 너는 내 하루를 보냈다 :-). 나는 오늘 새로운 것을 배웠다. – sand

관련 문제