<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scope | Bogdan's blog</title>
	<atom:link href="https://blog.bogdancarpean.com/tag/scope/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.bogdancarpean.com</link>
	<description>Code and anything else.</description>
	<lastBuildDate>Wed, 10 May 2017 23:15:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>

<image>
	<url>https://blog.bogdancarpean.com/wp-content/uploads/2022/12/icons8-pencil-drawing-96.png</url>
	<title>scope | Bogdan's blog</title>
	<link>https://blog.bogdancarpean.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Angular.js share variables between controllers with service</title>
		<link>https://blog.bogdancarpean.com/angular-js-share-variables-between-controllers-with-service/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=angular-js-share-variables-between-controllers-with-service</link>
					<comments>https://blog.bogdancarpean.com/angular-js-share-variables-between-controllers-with-service/#respond</comments>
		
		<dc:creator><![CDATA[Bogdan]]></dc:creator>
		<pubDate>Wed, 10 May 2017 23:15:43 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[angular]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[directive]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[share variables]]></category>
		<guid isPermaLink="false">http://blog.bogdancarpean.com/?p=69</guid>

					<description><![CDATA[<p>Note: This article applies to Angular 1 and the example is written in ES5. Let&#8217;s say you have a directive where you have a button and once clicked you want an event to happen in another directive. The problem is that the two directives have different scopes, so passing the change directly is not possible. [&#8230;]</p>
The post <a href="https://blog.bogdancarpean.com/angular-js-share-variables-between-controllers-with-service/">Angular.js share variables between controllers with service</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></description>
										<content:encoded><![CDATA[<p><em>Note: This article applies to Angular 1 and the example is written in ES5.</em></p>
<p>Let&#8217;s say you have a directive where you have a button and once clicked you want an event to happen in another directive. The problem is that the two directives have different scopes, so passing the change directly is not possible. In this approach, we are going to use a service to pass the data between the controllers of the two directives.</p>
<p>For example, we are going to take a directive that has a button that once clicked we display a custom modal. The modal is another directive not related to the one where the click originated.</p>
<p>First, we are going to write the service:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js" data-enlighter-title="dataService.js">"use strict";

angular.module("someApp").service("dataService", function () {

    var _isModalClicked  = false;

    this.setModalClicked = function(flag){
        _isModalClicked = flag;
    };

    this.getModalClicked = function(){
        return _isModalClicked;
    };
});</pre>
<p>Then, from the controller sending the change (the one attached to button directive), we are going to set the variable on the service (don&#8217;t forget to import the service into the controller):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">...
$scope.buttonClick = function(ev){
    dataService.setModalClicked(true);
}
...

</pre>
<p>At this point, we have wired the controller to the service and changed the state of _isModalClicked to true. What is left is to listen to that change inside of the controller consuming the event and to propagate the change, leading to showing the modal. The consuming controller will look like this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">'use strict';

angular.module('someApp')
    .controller('modalCtrl', function ($scope, dataService) {

        $scope.$watch(function() { return dataService.getModalClicked(); }, function(newVal) {
            $scope.showModal = newVal;
        }, true);
    });</pre>
<p>This way, you have a clear separation of concerns when you want to pass a variable change between scopes.</p>The post <a href="https://blog.bogdancarpean.com/angular-js-share-variables-between-controllers-with-service/">Angular.js share variables between controllers with service</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.bogdancarpean.com/angular-js-share-variables-between-controllers-with-service/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
