angular.js - Please tell me about the communication issues between controllers in angularjs
仅有的幸福
仅有的幸福 2017-05-15 16:59:16
0
1
556

I am working on a Q&A system, which has a question page that lists the question information and then lists the answers under the question. I used the QuestionCtrl controller for this single question page, then used http.get to get the answers, and used ng-repeat to list each answer. Then I use the same controller AnswerCtrl to control each answer. Each answer has a like function. When I first get the answer, I will get the answer.prised_counter to display, that is, the answer is currently liked. quantity. Then I add ng-click to the a tag of the like element to perform the like operation. At this time, the function in AnswerCtrl is called. After likes, I want to add answer.prised_counter 1, but because answer is from QuestionCtrl It is read from the answers in , so I cannot modify the answer.prised_counter variable in AnserCtrl. Please tell me how should I achieve this situation? Is the plan wrong?

仅有的幸福
仅有的幸福

reply all(1)
習慣沉默

In this case, one QuestionCtrl is enough. There is no need to create another AnswerCtrl, which complicates the problem.
If you must use answerCtrl, you can use $emit and $brodcast to implement parent-child scope communication.
Example

<ul ng-controller='questionCtrl'>
    <li ng-repeat="item in list" ng-click='like(item)'></li>
</ul>
   app.controller('questionCtrl',function(){
    $scope.list=[
    {count:10,content:"这里是答案1"},
    {count:12,content:"这里是答案2"}
    ]
    $scope.like=function(item){
        //answer function
    }
})
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template