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;
});
jsfiddleに問題を再現できますか? – 6220119
私が望むのは、私がControllerOneにいる間にemp.idUserをキャプチャして、それをcontrollerTwoで利用できるようにすることです。 – BumbleBee
私はjsfiddleを求めている理由は、タイプミスや些細な間違いを認識していないかもしれないからです。 'ng-click =" storeIds({{emp.idUser}}) 'を' ng-click = "storeIds(emp.idUser)に変更することができます。 ---- ' $ scope.storeIds '関数を呼び出し、データをサービスに割り当てます。 '$ scope.storeIds = function(ユーザID){ testService.UserId = userid; } ' – 6220119