我试图从一个控制器发送到angularjs中的其他控制器但是没有工作

时间:2017-06-02 17:18:45

标签: angularjs

/*service */
 app.service('sharedProperties', function () {
        var property = 'First';

        return {
            getProperty: function () {
                return property;
            },
            setProperty: function(value) {
                property = value;
            }
        };
    });


/*first contoller */
app.controller('loginCtrl',function($scope,$location,$http,$window,sharedProperties){
    $scope.submit =function(){
        var username=$scope.username;
        var pass=$scope.password;
      sharedProperties.setProperty(username);          


 $location.path('/userdashboard');
 $window.location.reload();
            });
        }  
    });

/*second controller*/
app.controller('empController', function($route,$scope,$http,$routeParams,sharedProperties){

    $scope.getEmployees = function(){

        alert( sharedProperties.getProperty());

     };    
   };

3 个答案:

答案 0 :(得分:0)

服务不需要返回任何内容。您必须在此变量中分配所有内容。因为服务将默认创建实例并将其用作基础对象。

app.service('sharedProperties', function () {
     this.property = 'First';
});

然后在控制器中

sharedProperties.property = $scope.username;

您是否希望使用factory

app.factory('sharedProperties', function () {
       var factory = {};
       factory.property = 'Hello';  

       factory.setProperty = function (value) {
            factory.property = value;
       };    
       return factory;
});

然后在控制器中你会使用它

sharedProperties.setProperty($scope.username);    // Setter
$scope.var = sharedProperties.property;  //getter

修改

Working Plnkr

答案 1 :(得分:0)

试试这个,它会对你有用。 :

        <div ng-app="myApp" ng-controller="myCtrl"> 

            <button ng-click="sendData();"></button>

        </div>


        <script>
            var app = angular.module('myApp', []);
            app.controller('myCtrl', function($scope, $http) {
                function sendData($scope) {
                    var arrayData = [1,2,3];
                    $scope.$emit('someEvent', arrayData);
                }

            });
            app.controller('yourCtrl', function($scope, $http) {
                $scope.$on('someEvent', function(event, data) {
                    console.log(data); 
                }); 
            });
        </script>

答案 2 :(得分:0)

您可以将成员分配给$ rootScope,后者将为您的应用全局存储数据。只需向每个控制器注入$ rootScope。

例如......

 /*first controller */        
app.controller('loginCtrl',function($scope,$rootScope,$location,$http,$window,sharedProperties){
            $scope.submit =function(){
                var username=$scope.username;
                var pass=$scope.password;
                $rootScope.username = username;          


                $location.path('/userdashboard');
                $window.location.reload();
            });
        }  
    });

这将使“用户名”可用于注入和使用$ rootScope的任何控制器。

/*second controller*/
app.controller('empController', function($route,$scope,$rootScope,$http,$routeParams,sharedProperties){

    $scope.getEmployees = function(){
        alert($rootScope.username);
     };    
   };