angularJS--多個控制器之間的資料共用,angularjs控制器

來源:互聯網
上載者:User

angularJS--多個控制器之間的資料共用,angularjs控制器

為了在控制器之間共用資料,需要在服務中添加一個用來儲存使用者名稱的方法。記住,服務在
應用的生命週期內是單例模式的,因此可以將使用者名稱安全地儲存在其中。

<!DOCTYPE html><html><head>    <meta  charset="utf-8">    <title>12-angularjs的多個控制器資料共用</title>    <script src="../js/angularjs.js"></script>    <script src="../js/index12.js"></script></head><body><!-- angularjs的資料繫結,雙向繫結,M,V,c之間的綁定 -->    <div ng-app="myApp">        <div ng-controller= "firstController">            <input type="text" ng-model="person.name" value="" />                firstData--{{person.name}}        </div>                <div ng-controller="secondController">            <input type="text"  value="" />                secondData--{{person.name}}        <div ng-controller= "firstController">            <input type="text" ng-model="Data.message" value="" />                firstData--{{Data.message}}        </div>                <div ng-controller="secondController">            <input type="text"  value="" />                secondData--{{Data.message}}        </div>        </div>    </div></body></html>
12345678910111213141516171819 var myApp = angular.module('myApp', []); myApp.factory('Data', function(){    return {message:'小平果'}; }); myApp.controller('firstController',  function($scope,Data){    $scope.person = {        name: "肖果平"    };     $scope.Data = Data;}); myApp.controller('secondController',  function($scope,Data){    $scope.person = $scope.$$prevSibling.person;//基本類型不能變的,只有對象才能引用    $scope.Data = Data;});

  

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.