在AngularJS控制器之間共享數據我正在嘗試跨控制器共享數據。用例是一種多步形式,在一個輸入中輸入的數據稍后用于原始控制器外的多個顯示位置。下面的代碼和jsfiddle這里。HTML<div ng-controller="FirstCtrl">
<input type="text" ng-model="FirstName"><!-- Input entered here -->
<br>Input is : <strong>{{FirstName}}</strong><!-- Successfully updates here --></div><hr><div ng-controller="SecondCtrl">
Input should also be here: {{FirstName}}<!-- How do I automatically updated it here? --></div>JS// declare the app with no dependenciesvar myApp = angular.module('myApp', []);
// make a factory to share data between controllersmyApp.factory('Data', function(){
// I know this doesn't work, but what will?
var FirstName = '';
return FirstName;});// Step 1 ControllermyApp.controller('FirstCtrl', function( $scope, Data ){});
// Step 2 ControllermyApp.controller('SecondCtrl', function( $scope, Data ){
$scope.FirstName = Data.FirstName;});任何幫助是極大的贊賞。
在AngularJS控制器之間共享數據
一只甜甜圈
2019-05-25 15:09:39