First user: {{firstUser}}
<!doctype html>
<html ng-app="MyApp"> <head> <script> var app = angular.module("MyApp", []); app.factory("UserService", function() { var users = ["A", "B", "C"]; return { all: function() { return users; }, first: function() { return users[0]; } }; }); app.controller("MyCtrl", function($scope, UserService) { $scope.users = UserService.all(); }); app.controller("AnotherCtrl", function($scope, UserService) { $scope.firstUser = UserService.first(); }); </script> </head> <body ng-app="MyApp"> <div ng-controller="MyCtrl"> <ul ng-repeat="user in users"> <li>{{user}}</li> </ul> <div class="nested" ng-controller="AnotherCtrl"> First user: {{firstUser}} </div> </div> </body> </html> |