Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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;
  }
});
share|improve this question
    
    
Why u saying another controller scope? there is only one controller – RIYAJ KHAN 12 hours ago

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

share|improve this answer

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

share|improve this answer
    
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 12 hours ago
1  
Its not working because MC.Entity scope is limited to div1. – Muzafar Khan 11 hours ago
    
Both controller are same.Is it type mistake or really same? If it is then ng-init=".." in div1 is meaningless – RIYAJ KHAN 11 hours ago
    
Both controllers are same. Its not a type mistake – Muzafar Khan 11 hours ago
    
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 11 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.