我正在try 在控制器之间共享数据.用例是一个多步骤的表单,在一个输入中输入的数据随后将用于原始控制器之外的多个显示位置.代码低于jsfiddle here.

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 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;
});

非常感谢您的帮助.

推荐答案

一个简单的解决方案是让工厂返回一个对象,并让控制器使用对同一对象的引用:

JS:

// declare the app with no dependencies
var myApp = angular.module('myApp', []);

// Create the factory that share the Fact
myApp.factory('Fact', function(){
  return { Field: '' };
});

// Two controllers sharing an object that has a string in it
myApp.controller('FirstCtrl', function( $scope, Fact ){
  $scope.Alpha = Fact;
});

myApp.controller('SecondCtrl', function( $scope, Fact ){
  $scope.Beta = Fact;
});

HTML:

<div ng-controller="FirstCtrl">
    <input type="text" ng-model="Alpha.Field">
    First {{Alpha.Field}}
</div>

<div ng-controller="SecondCtrl">
<input type="text" ng-model="Beta.Field">
    Second {{Beta.Field}}
</div>

Demo: http://jsfiddle.net/HEdJF/

当应用程序变得更大、更复杂、更难测试时,您可能不希望以这种方式从工厂公开整个对象,而是通过getter和setter等方式提供有限的访问权限:

myApp.factory('Data', function () {

    var data = {
        FirstName: ''
    };

    return {
        getFirstName: function () {
            return data.FirstName;
        },
        setFirstName: function (firstName) {
            data.FirstName = firstName;
        }
    };
});

通过这种方法,由使用控制器使用新值更新工厂,并观察更改以获得新值:

myApp.controller('FirstCtrl', function ($scope, Data) {

    $scope.firstName = '';

    $scope.$watch('firstName', function (newValue, oldValue) {
        if (newValue !== oldValue) Data.setFirstName(newValue);
    });
});

myApp.controller('SecondCtrl', function ($scope, Data) {

    $scope.$watch(function () { return Data.getFirstName(); }, function (newValue, oldValue) {
        if (newValue !== oldValue) $scope.firstName = newValue;
    });
});

HTML:

<div ng-controller="FirstCtrl">
  <input type="text" ng-model="firstName">
  <br>Input is : <strong>{{firstName}}</strong>
</div>
<hr>
<div ng-controller="SecondCtrl">
  Input should also be here: {{firstName}}
</div>

Demo: http://jsfiddle.net/27mk1n1o/

Javascript相关问答推荐

在具有焦点和上下文的d3多线图表中,如何将上下文的刷新限制在特定日期?

如何解决这个未能在响应上执行json:body stream已读问题?

过滤对象数组并动态将属性放入新数组

我应该绑定不影响状态的函数吗?'

Snowflake JavaScript存储过程返回成功,尽管预期失败

google docs boldText直到按行执行应用脚本错误

为什么promise对js中的错误有一个奇怪的优先级?

Google maps API通过API返回ZERO_RESULTS,以获得方向请求,但适用于Google maps

CheckBox作为Vue3中的一个组件

setcallback是什么时候放到macrotask队列上的?

使用JQuery单击元素从新弹出窗口获取值

try 使用javascript隐藏下拉 Select

为什么JPG图像不能在VITE中导入以进行react ?

如何在 Select 文本时停止Click事件?

在画布中调整边上反弹框的大小失败

为什么在函数中添加粒子的速率大于删除粒子的速率?

按什么顺序接收`storage`事件?

在D3条形图中对具有相同X值的多条记录进行分组

如何在Java脚本中对数据进行签名,并在PHP中验证签名?

Socket.IO在刷新页面时执行函数两次