search

Home  >  Q&A  >  body text

angular.js - angular directive scope issue

How to transfer the data generated by the controller inside the instruction to the controller outside the instruction

我想大声告诉你我想大声告诉你2816 days ago619

reply all(4)I'll reply

  • 漂亮男人

    漂亮男人2017-05-15 17:00:21

    Let’s talk about three methods first:

    1. The one upstairs answered using broadcast communication, $emit向上,$broadcastdownside

    2. The data of
    3. service共享数据,就是把同一个service注入到directivecontroller中,然后操作这个service is good

    4. Of course your directive如果在controller的里面,本身就可以访问到controller的作用域(前提是没创建独立scope),直接在directivecontroller中操作scope is fine

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-15 17:00:21

    Internal $scope.$emit("emit",data)
    External $scope.$on("emit",function(ev,data){console.log(data)})

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-15 17:00:21

    Use independent scope, "=" two-way binding, and pass the data you want to bind through the parameters in the instruction.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-15 17:00:21

    There are many ways to use your data.

    Distribution via event subscriptions and broadcasts

    //$rootScope
    $rootScope.$on('data-pass',function(event, data){ $rootScope.$broadcast('data-receive', data) })
    // 传递数据的controller
    $scope.$emit('data-pass', data)
    // 需要数据的controller
    $scope.$on('data-receiver', function(event, data){
        // use data to do something
    })

    Rewrite the object attribute value on the root scope through the inheritance feature of $scope

    // 根作用域
    $rootScope.data = {}
    // 传递数据的controller
    $scope.data.record = {}
    // 需要数据的controller
    // use $scope.data.record to do something

    Use the angular public module for data storage and inject it into the controller that needs to be used

    angular.factory('publicData',function(){
        return {}
    });
    // 传递数据的controller
    angular.controller('passController',function($scope, publicData){
        publicData.record = {}
    })
    // 需要数据的controller
    angular.controller('needController',function($scope, publicData){
        // use publicData.record to do something
    })

    reply
    0
  • Cancelreply