2

I have Following div

<div ng-controller="MyController as MC" id="div1">                                                                                                                   
    <a href="#" id="1" ng-init="MC.EntityId = 1 , MC.EntityType = 57" ng-click="MC.LoadFiles(MC.EntityId, MC.EntityType)" title="Upload">Upload </a>                                                                                                         
</div>

I want to display here EntityId and EntityType that i have set in div1

<div ng-controller="MyController as MC" id="div2">                                                                                                                   
     EntityId = {{MC.EntityId}}, EntityType = {{MC.EntityType}}                                                                                                      
</div>

How do i set EntityId and EntityType for div2 in LoadFiles function without using angular.element.

app.controller('MyController', function () {
  this.EntityId = 0;
  this.EntityType = 0;

  this.LoadFiles = function (id, type){
       this.EntityId = id;
       this.EntityType  = type;
  }
});
Muzafar Khan
  • 826
  • 3
  • 15
  • 28
  • related, possible duplicate [What's the correct way to communicate between controllers in AngularJS](http://stackoverflow.com/questions/11252780/whats-the-correct-way-to-communicate-between-controllers-in-angularjs) – Patrick Evans Feb 12 '16 at 06:10
  • Why u saying another controller scope? there is only one controller – RIYAJ KHAN Feb 12 '16 at 06:11

2 Answers2

3

You should create a service, then inject it to both of your controller, then save and retrieve your Entities in that service

Louie Almeda
  • 5,366
  • 30
  • 38
1

You need to use an object in order to do two-way data binding. Two way data binding is not work with primitive type.

Do following changes.

app.controller('MyController', function () {

  this.Entity = {EntityId:0,EntityType:0};

  this.LoadFiles = function (objEntity){
       this.Entity.EntityId = objEntity.EntityId;
       this.Entity.EntityType  = objEntity.EntityType;
  }
});

HTML :

<div ng-controller="MyController as MC" id="div1">
    <a href="#" id="1" ng-init="MC.Entity.EntityId = 1 , MC.Entity.EntityType = 57" ng-click="MC.LoadFiles(MC.Entity)" title="Upload">Upload </a>    
</div>

HTML Div2

<div ng-controller="MyController as MC" id="div2">                                                                                                                   
     EntityId = {{MC.Entity.EntityId}}, EntityType = {{MC.Entity. ntityType}}                                                                                                      
</div>

EDIT :

<div ng-controller="MyController as MC" id="div1">
    <a href="#" id="1"  ng-click="MC.LoadFiles(MC.Entity)" title="Upload">Upload </a>    
</div>

<div ng-controller="MyController as MC" id="div2">                                                                                                                   
     EntityId = {{MC.Entity.EntityId}}, EntityType = {{MC.Entity. EntityType}}                                                                                                      
</div>

Controller :

controller('MyController', function () {

  this.Entity = {EntityId:1,EntityType:57};

  this.LoadFiles = function (objEntity){
       this.Entity.EntityId = objEntity.EntityId;
       this.Entity.EntityType  = objEntity.EntityType;
  }
})

Plunker check

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
  • `You need to use an object in order to do two-way data binding. Two way data binding is not work with primitive type.` It work if you use `$scope.EntityId` ( i never use this ) – AlainIb Feb 12 '16 at 06:22
  • 1
    Its not working because MC.Entity scope is limited to div1. – Muzafar Khan Feb 12 '16 at 07:14
  • Both controller are same.Is it type mistake or really same? If it is then ng-init=".." in div1 is meaningless – RIYAJ KHAN Feb 12 '16 at 07:27
  • Both controllers are same. Its not a type mistake – Muzafar Khan Feb 12 '16 at 07:37
  • friend then why you needed click event to call and update same variable which come under same controller.?Why you need ng-init in DOM? – RIYAJ KHAN Feb 12 '16 at 07:38
  • friend :). Actually i want to load a bootstrap model on clicking upload. There are multiple model that are decided on the basis of Entityid. Sorry i don't mention all the code here. If i set entityid in my controller then on the model it will show the same id all the time. – Muzafar Khan Feb 12 '16 at 09:52
  • Thanx for your suggested code example and let me know if u have solution to my question... – Muzafar Khan Feb 12 '16 at 09:53