2

I have a single controller in my application http://jsfiddle.net/HKF9h/:

<div ng-controller="MyCtrl">
    All Items:
    <ul>
        <li ng-repeat="item in items">{{item }} - 
            <a href="" ng-click="like(item)">like it</a>
        </li>
    </ul>
    Items you liked:
    <ul>
        <li ng-repeat="item in likedItems">{{item }}
        </li>
    </ul>
</div>

<script type="text/javascript">
var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.items= ['foo', 'bar', 'z'];
    $scope.likedItems = [];

    $scope.like = function (item) {
        if($scope.likedItems.indexOf(item) === -1) {
            $scope.likedItems.push(item);         
    }
}
</script>
}

I would like move all 'favoriting' functionality into sep controller that can be reused later. Here's what i did, but repeater is not showing likedItems anymore. What did I do wrong? http://jsfiddle.net/MR8wz/

<div ng-controller="MyCtrl">
    All Items:
    <ul>
        <li ng-repeat="item in items">{{item }} - 
            <a href="" 
               ng-controller="FavoriteCtrl" 
               ng-click="like(item)">like it</a>
        </li>
    </ul>
    Items you liked:
    <ul ng-controller="FavoriteCtrl">
        <li ng-repeat="item in likedItems">{{item }} //this one does not trigger
        </li>
    </ul>
</div>

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

function MyCtrl($scope) {
    $scope.items= ['foo', 'bar', 'z'];
}

function FavoriteCtrl($scope) {
    $scope.likedItems = [];
    $scope.like = function (item) {
        console.log('you liked', item); //this one is displayed
        if($scope.likedItems.indexOf(item) === -1) {
            $scope.likedItems.push(item);         
        }
    }
}
3
  • Not related but if you use ng-app='myApp' and you set a module ref to it : var myAppModule = angular.module('myApp', []); so please attach the controller to it also : myAppModule.controller('TextController',... this is the whole idea of not pulliting the global namespace Commented Jan 12, 2014 at 9:23
  • Seems like controllers are not intended for reuse, are they? Commented Jan 12, 2014 at 9:27
  • Yes, controllers are not for reuse, controllers are tightly couple with templates. Commented Jan 12, 2014 at 9:40

2 Answers 2

2

You shouldn't really be relying on controller inheritance for sharing data. It's a bad case of close coupling. In Angular, if you want to share data, you're advised to use services/factories because:

  1. They're easily injectable anywhere you need them
  2. They're easily mockable
  3. They're easily testable

FIDDLE

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

myApp.factory('Favorite', function(){
    var likedItems = [];

    function like(item) {
        console.log('you liked', item); //this one is displayed
        if(likedItems.indexOf(item) === -1) {
            likedItems.push(item);         
        }
    }

    function getLikedItems(){
        return likedItems;
    };

    return {
        like: like,
        getLikedItems: getLikedItems
    };
});

function MyCtrl($scope, Favorite) {
    $scope.favorite = Favorite;
    $scope.items = ['foo', 'bar', 'z'];
}

function FavoriteCtrl($scope, Favorite) {
    $scope.likedItems = Favorite.getLikedItems();
}
<div ng-controller="MyCtrl">
    All Items:
    <ul>
        <li ng-repeat="item in items">{{item }} - 
            <a href="" 
               ng-controller="FavoriteCtrl" 
               ng-click="favorite.like(item)">like it</a>
        </li>
    </ul>
    Items you liked:
    <ul ng-controller="FavoriteCtrl">
        <li ng-repeat="item in likedItems">{{item }}
        </li>
    </ul>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

off-topic ,related , can u please have a look at this
So in this case we don't really need FavoriteCtrl in this view, do we? jsfiddle.net/DFus9/1
No, you don't. But the example is there to show how to share data between controllers, without relying on controller inheritance.
0

You use ng-controller directive two times

<a href="" ng-controller="FavoriteCtrl" ng-click="like(item)">like it</a>

and

<ul ng-controller="FavoriteCtrl">

It creates two different scope this is the reason it doesn't work. So if you move

$scope.likedItems = []; in parent controller MyCtrl then it works. Updated fiddle

1 Comment

Thanks, but there's no real reuse, because I need likedItems array in FavoriteCtrl as well. jsfiddle.net/MR8wz/4

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.