angular.js - angular指令 作用域问题
我想大声告诉你
我想大声告诉你 2017-05-15 16:58:21
0
4
583

如何让指令内部的controller生成的数据传到指令外部的控制器中

我想大声告诉你
我想大声告诉你

reply all(4)
漂亮男人

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

漂亮男人

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

世界只因有你

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

仅有的幸福

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
})
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template