2016-08-22 4 views
2

범위 변수를 $ scope에서 호출하지 않고 함수에 전달하면 범위 변수를 제거하는 방법은 무엇입니까?AngularJS 함수에서 범위 변수를 제거하십시오.

// controller 
$scope.user_id = 1; 

$scope.example = function(uid) { 
    // remove $scope.user_id without accessing it like a scope 
    // smth like 

    uid = null; // won't work 
}; 

// html 
<div ng-click="example(user_id)">Click me!</div> 

그래서 나는 완전히 격리 된 기능은 $scope에서 속성을 제거 할 수있을거야 유일한 방법은 delete 경유

답변

2

를 갖고 싶어. 동적 객체 (예 : $scope)과 속성을 참조해야하는 경우,

ng-click="example(this, 'user_id')" 

$scope.example = function(scope, key) { 
    delete scope[key]; 
}; 
+0

좋아 내가 볼 예를 들어, 문자열로 키를 전달하려고합니다. 고맙습니다! – user256968

+0

@NG 더 많은 "고립 된"* 예제를 추가했습니다. – Phil