0

지시어에서 범위를 'true'로 사용하고 있습니다. 이제는이 지시어 범위가 부모에서 자식으로 전달되지만 반대는 아닙니다. scope.name을 2 번 인쇄하고 있습니다. 부모 범위에서 첫 번째, 지시어에서 두 번째. 이제 2 가지 다른 값을 얻어야합니다. 그러나, 나는 둘 다 동일한 범위 값을 얻고 있습니다. 도와주세요!범위 지시문에 잘못된 결과가 나타납니다.

//module declaration 
 
var app = angular.module('myApp',[]); 
 

 
//controller declaration 
 
app.controller('myCtrl',function($scope){ 
 
$scope.name = "Peter"; 
 
}); 
 

 
//app declaration 
 
app.directive('myStudent',function(){ 
 

 
return{ 
 
\t template: "{{name}}", 
 
\t scope:true 
 
} 
 
controller: [function(){ 
 
\t $scope.name = "Roger" 
 
}] 
 

 
});
<body ng-app="myApp" ng-controller="myCtrl"> 
 

 
{{name}}, 
 

 
<my-student></my-student> 
 

 
</body> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>

답변

1

컨트롤러가 지침을 벗어났습니다. 나는 그것을 안에 배치하고 $scope 의존성을 추가했습니다. 그것은 작동합니다! 이 지시어의 즉 VAR와 바인더 제본 할 수 있도록

//module declaration 
 
var app = angular.module('myApp',[]); 
 

 
//controller declaration 
 
app.controller('myCtrl',function($scope){ 
 
$scope.name = "Peter"; 
 
}); 
 

 
//app declaration 
 
app.directive('myStudent',function(){ 
 

 
return{ 
 
\t template: "{{name}}", 
 
\t scope:true, 
 
     controller: ['$scope', function($scope) { 
 
      $scope.name = "Roger" 
 
     }] 
 
} 
 

 

 
});
<body ng-app="myApp" ng-controller="myCtrl"> 
 

 
{{name}}, 
 

 
<my-student></my-student> 
 

 
</body> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>

+0

완벽하게 실행됩니다! 감사. – Deadpool

+0

@Deadpool 도움이 될 경우 대답을 수락하십시오 – tanmay

+0

시간이 남았습니다. – Deadpool

1

은 DDO의 내부 컨트롤러를 작성합니다.

//app declaration 
app.directive('myStudent',function(){ 

return{ 
    template: "{{name}}", 
    scope:true, 
    controller: ['$scope', function($scope){ 
     $scope.name = "Roger" 
    }] 
} 

아마도이 방법이 문제를 해결할 것입니다.

+0

또한 지시어 컨트롤러에 $ scope를 주입해야합니다. 그렇지 않으면 오류가 발생합니다. 또한, DDO의 전체 형태는 무엇입니까? – Deadpool

+0

@Deadpool, 고마워, 나는 그것을 잊었다. – SaiUnique

+1

@Deadpool "Directive Definition Object" – tanmay

0

용이하여 상위 제어기 오브젝트을 X = {}; x.value = value; // 전달한 다음 액세스하려고합니다.

관련 문제