AngularJS 将变化的值从控制器传递到指令控制器
AngularJS Passing changing values from Controller to a Directive Controller
我是 angular 的新手,我在这里阅读了很多主题并用谷歌搜索了这个主题,但我无法得到明确的答案。我想要做的是传递一个在用户做出选择之前未设置的值,此时我的控制器将调用异步调用并将结果分配给控制器中的值。我的指令的控制器需要访问此值以执行其逻辑。
这是一些与我所拥有的类似的代码。
app.controller('testCtrl', function($scope){
$scope.getData = function(){
//getDataFunc is a method in a Factory, which is not shown here
$scope.results = getDataFunc();
}
}
app.directive('testDir', function(){
return{
restrict: 'AE',
scope: {
resultData: '='
},
controller:['$scope', function($scope){
//I need to be able to access the $scope.results from the parent controller
}
}
}
如有任何帮助,我们将不胜感激。
你很接近:
您的标记应如下所示:
<div ng-controller="testCtrl">
<test-dir result-data="results"></test-dir>
</div>
app.controller('testCtrl', function($scope){
$scope.getData = function(){
//getDataFunc is a method in a Factory, which is not shown here
$scope.results = getDataFunc();
}
}
app.directive('testDir', function(){
return{
restrict: 'AE',
scope: {
resultData: '='
},
controller:['$scope', function($scope){
//I need to be able to access the $scope.results from the parent controller
//$scope.resultData will always reflect the value of results here
$scope.$watch('resultData', function(){
//called any time $scope.resultData changes
});
}
}
}
您实际上不需要双向绑定,所以这就是我的实际做法:
app.directive('testDir', function(){
return{
restrict: 'AE',
scope: {
resultData: '&'
},
controller:['$scope', function($scope){
//I need to be able to access the $scope.results from the parent controller
//calling $scope.resultData() will always return the current value of the parent $scope.results
$scope.$watch('resultData()', function(){
//called any time $scope.resultData() changes
});
}
}
}
使用值和 set/get 方法创建一个服务来存储 getDataFunc() 的结果,然后将该服务注入指令以获取对值的访问。
我是 angular 的新手,我在这里阅读了很多主题并用谷歌搜索了这个主题,但我无法得到明确的答案。我想要做的是传递一个在用户做出选择之前未设置的值,此时我的控制器将调用异步调用并将结果分配给控制器中的值。我的指令的控制器需要访问此值以执行其逻辑。
这是一些与我所拥有的类似的代码。
app.controller('testCtrl', function($scope){
$scope.getData = function(){
//getDataFunc is a method in a Factory, which is not shown here
$scope.results = getDataFunc();
}
}
app.directive('testDir', function(){
return{
restrict: 'AE',
scope: {
resultData: '='
},
controller:['$scope', function($scope){
//I need to be able to access the $scope.results from the parent controller
}
}
}
如有任何帮助,我们将不胜感激。
你很接近:
您的标记应如下所示:
<div ng-controller="testCtrl">
<test-dir result-data="results"></test-dir>
</div>
app.controller('testCtrl', function($scope){
$scope.getData = function(){
//getDataFunc is a method in a Factory, which is not shown here
$scope.results = getDataFunc();
}
}
app.directive('testDir', function(){
return{
restrict: 'AE',
scope: {
resultData: '='
},
controller:['$scope', function($scope){
//I need to be able to access the $scope.results from the parent controller
//$scope.resultData will always reflect the value of results here
$scope.$watch('resultData', function(){
//called any time $scope.resultData changes
});
}
}
}
您实际上不需要双向绑定,所以这就是我的实际做法:
app.directive('testDir', function(){
return{
restrict: 'AE',
scope: {
resultData: '&'
},
controller:['$scope', function($scope){
//I need to be able to access the $scope.results from the parent controller
//calling $scope.resultData() will always return the current value of the parent $scope.results
$scope.$watch('resultData()', function(){
//called any time $scope.resultData() changes
});
}
}
}
使用值和 set/get 方法创建一个服务来存储 getDataFunc() 的结果,然后将该服务注入指令以获取对值的访问。