Search code examples
javascriptangularjsangularjs-scope

How to pass a restricted scope to a widget in AngularJS


I have the scope of my main controller as :

$scope.names=[];
$scope.articles=[];
$scope.whatever=[];

I want to have a Directive/Controller that should only list "names" (and I don't want it to be able to access others). Can I, and if yes how to, create and link this scope (restricted to names array) to this Directive/Controller?


Solution

  • Create a directive called names and pass this to the isolated scope of directive.

    <Names names="names"></Names>
    

    And then in your directive, you can access it like this.

    angular.module("yourapp").directive('Names', function(){
            return {
                 scope : { names :"=" }
            }    
    });