How to pass parameter by value into controller methods?

361 views
Skip to first unread message

satyajeet anand tripathy

unread,
Sep 18, 2012, 8:23:17 AM9/18/12
to ang...@googlegroups.com
Hi ,

How would i be able to send data by value.


for example:
<script>
var data="Hello world";
<script>
<input type="button" ng-click="echo(data) ">

$scope.echo(data)
{
console.log(data);

}

Is it possible to send data into methods defined in controller from outside controller .

Thanks
Satyajeet



James Wright

unread,
Sep 18, 2012, 8:57:52 AM9/18/12
to ang...@googlegroups.com
you can do this (If I understand you)

$scope.callMe = function(something){
     alert(something);
};

satyajeet anand tripathy

unread,
Sep 18, 2012, 9:15:03 AM9/18/12
to ang...@googlegroups.com
This will throw error when the page loads. The angular framework will throw error : "something" is not defined.

I guess inside controller the methods don't accept  parameter declarations.

James Wright

unread,
Sep 18, 2012, 10:09:27 AM9/18/12
to ang...@googlegroups.com
throw up a jsfiddle so we can see what's going on


On Tuesday, September 18, 2012 8:23:17 AM UTC-4, satyajeet anand tripathy wrote:

satyajeet anand tripathy

unread,
Sep 18, 2012, 11:22:09 AM9/18/12
to ang...@googlegroups.com
Hi James,


The alert is coming but the data fed is undefined.

Thanks
Satyajeet

Ricardo Bin

unread,
Sep 18, 2012, 11:48:12 AM9/18/12
to ang...@googlegroups.com
In your fiddle, the variables are in differents scopes and you are trying to use globals variables as parameter to angular controller's methods.

There a workaround to do this, but i dont recommend use this way.


A better way is consuming it as angular service:

Marco Alves

unread,
Sep 18, 2012, 11:51:29 AM9/18/12
to ang...@googlegroups.com
The data variable has to be exposed in the $scope. Since it's not, it comes out as undefined.

satyajeet anand tripathy

unread,
Sep 19, 2012, 7:36:14 AM9/19/12
to ang...@googlegroups.com
Thanks All, this work around helped. Is there  a way  to push changes in model from DOM events or by a function call from Window scope.  I have built samples with timeout/XHR but DOM seems bit tricky . 
Reply all
Reply to author
Forward
0 new messages