Example for stackoverflow. Share data between service

useFormError is a angularjs directive, which is designed to create custom checks

by Stepan Kasyanenko

HTML

<script src="https://cdn.rawgit.com/Stepan-Kasyanenko/use-form-error/master/src/use-form-error.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-messages.min.js"></script>
<script src="https://rawgit.com/mgcrea/angular-strap/master/dist/angular-strap.min.js"></script>
<script src="https://rawgit.com/mgcrea/angular-strap/master/dist/modules/modal.min.js"></script>
<script src="https://rawgit.com/mgcrea/angular-strap/master/dist/modules/modal.tpl.min.js"></script>
<script src="https://rawgit.com/mgcrea/angular-strap/master/dist/angular-strap.tpl.min.js"></script>
<body ng-app="myApp">
  <div ng-controller="ExampleOneController">
    <h2>
  ExampleOneController
  </h2>
    <div ng-repeat="n in news">
      <textarea ng-model="n.theme"></textarea>
    </div>
    <input ng-model="newVar.val">
  </div>
  <div ng-controller="ExampleTwoController">
    <h2>
  ExampleTwoController
  </h2>
    <div ng-repeat="n in news">
      <div>{{n.theme}}</div>
    </div>
    <pre>newVar from ExampleOneController {{anotherVar.val}}</pre>
  </div>
</body>

CSS

label.required:after {
  content: ' *';
  color: #f00;
}

JavaScript

var myApp = angular.module("myApp", []);


myApp.controller("ExampleOneController", function($scope, NewsService) {
  $scope.newVar = {
    val: ""
  };
  NewsService.newVar = $scope.newVar;
  $scope.news = NewsService.news;
});
myApp.controller("ExampleTwoController", function($scope, NewsService) {
  $scope.anotherVar = NewsService.newVar;
  $scope.news = NewsService.news;
});

myApp.service("NewsService", function() {
  return {
    news: [{
      theme: "This is one new"
    }, {
      theme: "This is two new"
    }, {
      theme: "This is three new"
    }]
  }
});