hmmm.... ok, first thing, I'm not sure if the listLength will be updated
just by pushing/popping an item into that array... Second unless the
controllers are nested I think there is no way to that variable be visible
on the other controllers.

Why don't you try to move all the array logic to a service and inject this
service on every controller... and then take the length directly from that
array...

app.service('mySpecialList', function(){
  return {list: []}
})

app.controller('ctrl1', function($scope, mySpecialList){
    $scope.myList = mySpecialList
})

app.controller('ctrl2', function($scope, mySpecialList){
    $scope.myList = mySpecialList
})

app.controller('ctrl3', function($scope, mySpecialList){
    $scope.myList = mySpecialList
})

Or use $broadcast and $on to share the length:

// on the controller1
$scope.list = []
$scope.$watch('list', function(){
  // note that you need to inject the $rootScope
  $rootScope.$broadcast('listUpdated', $scope.list.length)
})

// on the other controllers
$scope.$on('listUpdated', function(length){
  $scope.listLength = length
})

Or the ugliest way to share this list is to include the $rootScope on every
controller and keep the list in the $rootScope so you'll have access to the
list on every controller.

Choose wisely :D



On Mon, Mar 28, 2016 at 4:29 PM, <[email protected]> wrote:

> Hi Pablo,
>
> There is quiet a bot of code but yes they are in nested controllers,
> here's some code. Once  the update function in controller 2 is done the
> listlength variable updates accordingly but the updated value doesn't carry
> over to the third controller, why is that?
>
> app.controller("controller1", function($scope){
> $scope.list = []; //this gets populated with data from a database
> $scope.listLength = $scope.list.length;
> });
>
>
> app.controller("controller2", function($scope){
>      $scope.$watch('listLength', function(newVal, oldVal){
>        if($scope.listLength !== newVal) $scope.listLength = newVal;
>      });
>  }); //I have watch functions within the controllers like so
>
>
>
> app.controller("controller3", function($scope){
>        $scope.$watch('listLength', function(newVal, oldVal){
>           if($scope.listLength !== newVal) $scope.listLength = newVal;
>        });
>
>
> });
>
>
>
>
>
>
> On Sunday, March 27, 2016 at 7:29:09 PM UTC+2, Pablo Madalena Targa wrote:
>>
>> Can you set a simple env of the three controllers and the interaction
>> between them, in pastebin, fiddlejs or codepen?
>>
>>  Are they nested controllers, how are you sharing the $scope?
>>
>> On Sun, Mar 27, 2016 at 5:59 PM, <[email protected]> wrote:
>>
>>> Hi Pablo,
>>>
>>> Yes I have tried that I have put watches in three of my controllers, the
>>> add, edit and delete but on the add it still doesn't update my array. Does
>>> it have to be placed in a particular place to work?
>>>
>>> On Sunday, March 27, 2016 at 4:50:04 PM UTC+2, Pablo Madalena Targa
>>> wrote:
>>>>
>>>> Hi friend,
>>>>
>>>> have you tried this?
>>>>
>>>> $scope.$watch('list.length', function(newValue, oldValue){...});
>>>>
>>>> On Sun, Mar 27, 2016 at 3:15 PM, <[email protected]> wrote:
>>>>
>>>>> I have an empty array defined as $scope.list = [] I then select items
>>>>> from the database and push them into the array as an object like so {
>>>>> name:"john", surname:"doe, age:35 } I have stored the length of the list 
>>>>> in
>>>>> an variable however upon deleting items or adding items to the array
>>>>> doesn't update it's length. What am I doing wrong? Here is my code
>>>>>
>>>>> $scope.list = [];
>>>>>  var scopeListLength = $scope.list.length;
>>>>>  $scope.$watch(scopeListLength, function(newVal, oldVal){
>>>>>  console.log(newVal);
>>>>>  console.log(oldVal);
>>>>>  if(newVal !== oldVal) scopeListLength = newVal;
>>>>>  console.log("scopeListLength = " + scopeListLength);
>>>>>  });
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "AngularJS" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>> Visit this group at https://groups.google.com/group/angular.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "AngularJS" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at https://groups.google.com/group/angular.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to