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;
  }
});
2

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

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

7 Comments

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 )
Its not working because MC.Entity scope is limited to div1.
Both controller are same.Is it type mistake or really same? If it is then ng-init=".." in div1 is meaningless
Both controllers are same. Its not a type mistake
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.