angularjs--data sharing between multiple controllers

Source: Internet
Author: User

In order to share data between controllers, you need to add a method in the service to store the user name. Remember, services are
The lifetime of the app is a singleton pattern, so you can safely store the user name in it.

<!DOCTYPE HTML><HTML><Head>    <MetaCharSet= "Utf-8">    <title>Multiple controller data sharing for 12-ANGULARJS</title>    <Scriptsrc=".. /js/angularjs.js "></Script>    <Scriptsrc=".. /js/index12.js "></Script></Head><Body><!--ANGULARJS Data binding, two-way binding, binding between M,v,c -    <DivNg-app= "MYAPP">        <DivNg-controller= "Firstcontroller">            <inputtype= "text"Ng-model= "Person.name"value="" />Firstdata--{{person.name}}</Div>                <DivNg-controller= "Secondcontroller">            <inputtype= "text"value="" />Seconddata--{{person.name}}<DivNg-controller= "Firstcontroller">            <inputtype= "text"Ng-model= "Data.message"value="" />Firstdata--{{data.message}}</Div>                <DivNg-controller= "Secondcontroller">            <inputtype= "text"value="" />Seconddata--{{data.message}}</Div>        </Div>    </Div></Body></HTML>
var myApp = angular.module (' MyApp ', []); Myapp.factory (' Data ', function () {return {message: ' Xiaoping '};}); Myapp.controller (' Firstcontroller ',  function ($scope, Data) {$scope. person = {Name: "Sho Goping"}; $scope. data = data;}); Myapp.controller (' Secondcontroller ',  function ($scope, Data) {$scope. person = $scope. $ $prevSibling. person;// The base type cannot be changed, only the object can reference $scope. data = data;});

  

angularjs--data sharing between multiple controllers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.