How to transfer the data generated by the controller inside the instruction to the controller outside the instruction
漂亮男人2017-05-15 17:00:21
Let’s talk about three methods first:
The one upstairs answered using broadcast communication, $emit
向上,$broadcast
downside
service
共享数据,就是把同一个service
注入到directive
和controller
中,然后操作这个service
is good
Of course your directive
如果在controller
的里面,本身就可以访问到controller
的作用域(前提是没创建独立scope),直接在directive
的controller
中操作scope
is fine
漂亮男人2017-05-15 17:00:21
Internal $scope.$emit("emit",data)
External $scope.$on("emit",function(ev,data){console.log(data)})
世界只因有你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.
仅有的幸福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
})