Tôi đang cố gắng chia sẻ dữ liệu trên các bộ điều khiển. Ca sử dụng là một hình thức gồm nhiều bước, dữ liệu được nhập vào một đầu vào sau đó được sử dụng ở nhiều vị trí hiển thị bên ngoài bộ điều khiển gốc. Mã dưới đây và trong jsfiddle ở đây .
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>
Mã não
// declare the app with no dependencies
var myApp = angular.module('myApp', []);
// make a factory to share data between controllers
myApp.factory('Data', function(){
// I know this doesn't work, but what will?
var FirstName = '';
return FirstName;
});
// Step 1 Controller
myApp.controller('FirstCtrl', function( $scope, Data ){
});
// Step 2 Controller
myApp.controller('SecondCtrl', function( $scope, Data ){
$scope.FirstName = Data.FirstName;
});
Bất kỳ trợ giúp nào cũng được đánh giá rất cao.