2016-05-03 19 views
0

サービス内の情報を永続化して、コントローラ間のデータにアクセスできるようにしようとしています。ここに私が試したものです:AngularJS ng-clickでサービス関数にデータを設定する方法

HTML:

<div ng-app="myApp"> 

    <div ng-controller="ControllerOne as one"> 
     <h2>ControllerOne:</h2> 
<table> 
<tbody> 
    <tr ng-repeat="emp in EmployeeInfo"> 
     <td>{{emp.name}}</td> 
     <td>{{emp.hireDate}}</td> 
     <td><a class="btn-sm btn-primary pull-right" ng-click="storeIds({{emp.idUser}})" >E-Verify</a></td> 
    </tr> 
    </tbody> 
</table> 
     Change testService.loginName: <input type='text' ng-model='one.myService.UserId'/> </br></br> 
     myName: {{one.myService.UserId}} 
    </div> 
    <hr> 
    <div ng-controller="ControllerTwo as two"> 
     <h2>ControllerTwo:</h2> 
     myName: {{two.myService.UserId}} 
    </div> 

</div> 

JS:

app.service('testService', function(){ 
    this.UserId= uId; 
}); 

app.controller('ControllerOne', function($scope, testService){ 
    $scope.storeIds = function (userid) { 
     // HERE I want to call the testService and set the value. 
    } 
    this.myService = testService; 
}); 

app.controller('ControllerTwo', function($scope, testService){ 
    this.myService = testService; 
}); 
+0

jsfiddleに問題を再現できますか? – 6220119

+0

私が望むのは、私がControllerOneにいる間にemp.idUserをキャプチャして、それをcontrollerTwoで利用できるようにすることです。 – BumbleBee

+0

私はjsfiddleを求めている理由は、タイプミスや些細な間違いを認識していないかもしれないからです。 'ng-click =" storeIds({{emp.idUser}}) 'を' ng-click = "storeIds(emp.idUser)に変更することができます。 ---- ' $ scope.storeIds '関数を呼び出し、データをサービスに割り当てます。 '$ scope.storeIds = function(ユーザID){ testService.UserId = userid; } ' – 6220119

答えて

0

あなたは$ rootScopeを使用してに探して試してみましたか?

app.controller('ControllerOne', function($scope, rootScope, testService){ 
$scope.storeIds = function (userid) { 


    // HERE I want to call the testService and set the value. 
} 
$rootScope.myService = userid; 
}); 

app.controller('ControllerTwo', function($scope, ,rootScope, testService){ 
//rootScope.myService will now equal whatever the userid was in the other controller. 
//I skipped using the service 

});

関連する問題