i new angularjs , ionic. building first app , want know, there way can inject expressions in main controller , use them in other controllers.
so instead of typing $scope, $http, $ionicpopup in every controller. want put in main controller , use them in other controllers.
var app=angular.module('app.controllers', []);
can tell me called?
we use service communicate between controllers.
in following sample example, create animalservice, , inject both controllers. use $watch monitor changes.
http://plnkr.co/edit/hfkuxjo2xzdb6msk7ief
(function () { angular.module("root", []) .controller("leftanimal", ["$scope", "animalservice", function ($scope, animalservice) { $scope.findnewanimal = function () { var randnum = math.floor(math.random() * animalpool.length); $scope.animal = animalpool[randnum]; console.log("left click index: " + randnum); animalservice.setanimal(randnum); }; $scope.$watch(function () { return animalservice.getanimal(); }, function (value) { $scope.animal = animalpool[value]; }); } ]) .controller("rightanimal", ["$scope", "animalservice", function ($scope, animalservice) { $scope.findnewanimal = function () { var randnum = math.floor(math.random() * animalpool.length); $scope.animal = animalpool[randnum]; console.log("right click index: " + randnum); animalservice.setanimal(randnum); }; $scope.$watch(function () { return animalservice.getanimal(); }, function (value) { $scope.animal = animalpool[value]; }); } ]) .factory("animalservice", [function () { var index = 0; function getanimal() { return index; } function setanimal(newindex) { index = newindex; } return { getanimal: getanimal, setanimal: setanimal, } }]); var animalpool = [{ name: "baby quetzal", img: "http://i.imgur.com/ctnedpm.jpg", baby: true }, { name: "baby otter", img: "http://i.imgur.com/1ishhrt.jpg", baby: true }, { name: "baby octopus", img: "http://i.imgur.com/kzarlkw.jpg", baby: true }]; })();
<!doctype html> <html ng-app="root"> <head> <meta charset="utf-8" /> <title>angularjs plunker</title> <link data-require="bootstrap-css@3.3.6" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" /> <script>document.write('<base href="' + document.location + '" />');</script> <link rel="stylesheet" href="style.css" /> <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script> <script src="app.js"></script> </head> <body> <div class="row"> <div class="text-center"> <h2>pick animal</h2> </div> <div ng-controller="leftanimal" class="col-md-6"> <div class="animalimage"> <img class="img-center" ng-src="{{animal.img}}"/> </div> <div class="animalname">{{animal.name}}</div> <div class="animaldescription">{{animal.description}}</div> <button type="button" ng-click="findnewanimal()" class="btn btn-info img-center"> change </button> </div> <div ng-controller="rightanimal" class="col-md-6"> <div class="animalimage"> <img class="img-center" ng-src="{{animal.img}}" /> </div> <div class="animalname">{{animal.name}}</div> <div class="animaldescription">{{animal.description}}</div> <button type="button" ng-click="findnewanimal()" class="btn btn-info img-center"> change </button> </div> </div> </body> </html>
Comments
Post a Comment