访问控制器内的指令范围变量



parentData 在父控制器中被分配了一个值。

testDir 指令正在父 html 中调用。

<test-dir directive-data="parentData"></test-dir>

测试目录指令:

app.directive('testDir', function () {
return {
restrict: 'E',
scope: {
directiveData: '='   
},
controller: 'myctrl',
templateUrl: 'view/myView.html'
};
});

myCtrl 控制器:

app.controller('myCtrl', ['$scope', function($scope) {
$scope.newVar = $scope.directiveData;
console.log($scope.directiveData); //printing undefined.
console.log($scope); //prints the scope objects, directiveData is having the correct value passed from the parent controller
console.log($scope.new); //undefined
/* remaining code using the variable newVar */
}]);

我的视图.html

<div>
{{directiveData}} <!-- displaying the proper value -->
</div>

我无法访问控制器内的指令范围,但是,它正在 html 中打印。

但是在使用超时或监视功能时,也可以在控制器中访问范围变量。

我假设首先创建控制器,然后指令进入图片。

我不想使用监视或超时功能来解决此问题。有什么建议吗?

提前感谢!

尝试在指令中添加 bindToController 和 controllerAs。

最新更新