D
D
Dauren S2021-10-14 22:23:58
Angular
Dauren S, 2021-10-14 22:23:58

Substitute value from one controller to another in Angularjs?

Hello,
tell me there is a code below, how to substitute the value from another controller into the #cqnum input when clicking on the button from (CController)

<div ng-controller="BController" ng-init="init()">
  <input type="text" id="cqnum" ng-model="cqnum"/>
  <button  ng-click="Create2()">Сохранить 2</button>
</div>
<div ng-controller="CController" ng-init="init()" style="height: 100%;width:100%;">	
<button  ng-click="Create1()">Сохранить 1</button>
</div>

<script>
  pqsAppModule.service('shareData', function() {
    return {
        setData : setData,
        getData : getData,
        shared_data : {} 
    }

    function setData(data) {
        this.shared_data = data
    }

    function getData() {
        return this.shared_data
    } 
})
</script>

<script type="text/javascript" charset="utf-8">
pqsAppModule.controller('BController',function($scope,shareData) {
  $scope.Create2 = function(stat){	
    console.log('send');	
    console.log(shareData.getData());
  }
});

</script>

<script type="text/javascript" charset="utf-8">
pqsAppModule.controller('CController',function($scope,shareData) {
  $scope.cq_id = 0;
  $scope.Create1 = function(stat){	
    $scope.cq_id= 123;
    shareData.setData(data=$scope.cq_id);
  }
});
</script>

That is, I get the value in (BController) and when I click on the button (BController) I see it, but I need to substitute the value from (CController) into the input (cqnum)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question