Angular指令隔离scope: scope函数won't execute
Angular directive isolate scope: scope function won't execute
我很难理解如何定义由(或内部)具有隔离作用域的指令使用的函数。在下面的代码中,为什么$scope.foo()
函数没有执行?有什么正确的方法来解决这个问题吗?我正在寻找一个按钮,只有当用户登录时才可见,并且认为指令将是封装/隔离该功能的好方法。
<!DOCTYPE html>
<html>
<head>
<script src="https://code.angularjs.org/1.4.2/angular.js"></script>
<script>
angular.module('MyApp', [])
.directive('myScopedDirective', function() {
return {
scope: {}, // <!-- isolate scope breaks things
controller: function($scope) {
// isolate scope prevents this function from being executed
$scope.foo = function() {
alert('myScopedDirective / foo()');
};
}
};
});
</script>
</head>
<body>
<div ng-app="MyApp">
<!-- button doesn't work unless isolate scope is removed -->
<button my-scoped-directive ng-click="foo()">directive container - foo()</button>
</div>
</body>
</html>
砰砰作响:http://plnkr.co/edit/Sm81SzfdlTrziTismOg6
它不工作,因为你在不同的范围内使用2个指令,ngClick
和myScopedDirective
。你需要为指令创建一个模板,并像这样调用click函数:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.angularjs.org/1.4.2/angular.js"></script>
<script>
angular.module('MyApp', [])
.directive('myScopedDirective', function() {
return {
restrict: 'AE', // Can be defined as element or attribute
scope: {},
// Call the click function from your directive template
template: '<button ng-click="foo()">directive container - foo()</button>',
controller: function($scope) {
$scope.foo = function() {
alert('myDirective / foo()');
};
}
};
});
</script>
</head>
<body>
<div ng-app="MyApp">
<my-scoped-directive></my-scoped-directive>
</div>
</body>
</html>
工作恰好相关文章:
- AngularJS:ng之后,重复$scope值未按预期更新
- $ionicplatform内的$scope不;不起作用
- 使用$scope方法时的ControllerAs语法
- 如何将ngrepeat下的ngmodel绑定到$scope
- 另一个if(!$scope.$$phase)$scope$apply()szenario-需要帮助才能通关
- 使用AngularJS Directive WHITOUT$scope创建一个动态html元素
- $scope变量被视为字符串AngularJs
- 骨干's Router.execute(callback,args,name)方法获取未定义的名称
- ng在更新$scope后重复不更新信息
- 正在获取Angular以检测$scope中的更改
- 如何惯用地手动销毁scope&在AngularJS中重新创建
- Understanding Javascript scope with "var that = this&qu
- AngularJS,angular.element($0).scope()揭示了每个控制器的函数
- Ng出口无法访问父$scope
- ng选项-用vm替换$scope
- $scope变量,ng隐藏/显示
- 强制 $scope.$watch 只开火一次
- Ionic:AngularJS变量未使用$scope更新DOM
- Angular$scope在模式窗口内不起作用
- Angular指令隔离scope: scope函数won't execute