angular中不同controller傳值問題


利用angularJS中service單例模式的特性,服務(service)提供了一種能在應用的整個生命周期內保持數據的方式,能夠在控制器之間進行通信,且能保證數據的一致性。

一般我們都會封裝server來為應用提供訪問數據的接口,或者跟遠程進行數據交互。

示例

JavaScript

var myApp = angular.module("myApp", []);
myApp.factory('Data', function() {
  return {
    name: "Ting"
  }
});

myApp.controller(
'FirstCtrl', function($scope, Data) {   $scope.data = Data;   $scope.setName = function() {     Data.name = "Jack";   } });
myApp.controller(
'SecondCtrl', function($scope, Data) {   $scope.data = Data;   $scope.setName = function() {     Data.name = "Moby";   } });

 


 

參考文章:《angularJS的controller之間如何正確的通信》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM