在尝试访问范围问题的角度,引导指令 [英] Issue in angular-bootstrap directive while trying to access scope
问题描述
我已经使用 https://github.com/angular-ui/bootstrap 手风琴,但这个指令我有范围的问题。它仅允许如果ngController手风琴内声明的使用范围。
请访问空两Plunker链接,你就会明白我想说的:
示例1 : http://plnkr.co/edit/Fb4UauWWmHOnTyjMPFBo
的index.html
<!DOCTYPE HTML>
< HTML NG-应用=plunker>
< HEAD>
&LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js\"></script>
&LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-animate.js\"></script>
&LT;脚本的src =// angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.4.js\"></script>
&所述; SCRIPT SRC =的script.js&GT;&下; /脚本&GT;
&LT;链接HREF =// netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css的rel =stylesheet属性&GT;
&LT; /头&GT;
&LT;身体GT;
&LT; DIV NG控制器=AccordionDemoCtrl&GT;
&LT;手风琴与GT;
&LT;手风琴组开=status.open&GT;
&LT;手风琴标题&GT;
手风琴 - 点击ME&LT; I类=右拉glyphiconNG-CLASS ={'glyphicon雪佛龙下:status.open,glyphicon - 雪佛龙权:status.open!}&GT;&LT / I&GT;
&LT; /手风琴标题&GT;
&LT; DIV&GT;
&LT;输入类型=复选框NG模型=选择NG点击=checkAll()/&GT;检查我
&LT; / DIV&GT;
&LT; /手风琴组&gt;
&LT; /手风琴与GT;
&LT; / DIV&GT;
&LT; /身体GT;
&LT; / HTML&GT;
的script.js
VAR应用= angular.module('plunker',['ui.bootstrap']);
app.controller('AccordionDemoCtrl',函数($范围){
$ scope.checkAll =功能(){
警报($ scope.select);
};
});
例2 : http://plnkr.co/edit/ljEMUnTqPBqUyub5eEB7
的index.html
&LT;!DOCTYPE HTML&GT;
&LT; HTML NG-应用=plunker&GT;
&LT; HEAD&GT;
&LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js\"></script>
&LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-animate.js\"></script>
&LT;脚本的src =// angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.4.js\"></script>
&所述; SCRIPT SRC =的script.js&GT;&下; /脚本&GT;
&LT;链接HREF =// netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css的rel =stylesheet属性&GT;
&LT; /头&GT;
&LT;身体GT;
&LT;手风琴与GT;
&LT;手风琴组开=status.open&GT;
&LT;手风琴标题&GT;
手风琴 - 点击ME&LT; I类=右拉glyphiconNG-CLASS ={'glyphicon雪佛龙下:status.open,glyphicon - 雪佛龙权:status.open!}&GT;&LT / I&GT;
&LT; /手风琴标题&GT;
&LT; DIV NG控制器=AccordionDemoCtrl&GT;
&LT;输入类型=复选框NG模型=选择NG点击=checkAll()/&GT;检查我
&LT; / DIV&GT;
&LT; /手风琴组&gt;
&LT; /手风琴与GT;
&LT; /身体GT;
&LT; / HTML&GT;
的script.js
VAR应用= angular.module('plunker',['ui.bootstrap']);
app.controller('AccordionDemoCtrl',函数($范围){
$ scope.checkAll =功能(){
警报($ scope.select);
};
});
我找到了解决办法:
我们可以从函数本身使用$范围传递的价值,没有必要访问值。
演示: http://plnkr.co/edit/fZZrDN4e8kbvimR2Wzya
的index.html
&LT;!DOCTYPE HTML&GT;
&LT; HTML NG-应用=plunker&GT;&LT; HEAD&GT;
&LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js\"></script>
&LT;脚本的src =// ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-animate.js\"></script>
&LT;脚本的src =// angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.4.js\"></script>
&所述; SCRIPT SRC =的script.js&GT;&下; /脚本&GT;
&LT;链接HREF =// netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css的rel =stylesheet属性&GT;
&LT; /头&GT;&LT;身体GT; &LT; DIV NG控制器=AccordionDemoCtrl&GT;
&LT;手风琴与GT;
&LT;手风琴组开=status.open&GT;
&LT;手风琴标题&GT;
手风琴 - 点击ME&LT; I类=右拉glyphiconNG-CLASS ={'glyphicon雪佛龙下:status.open,glyphicon - 雪佛龙权:status.open!}&GT;&LT / I&GT;
&LT; /手风琴标题&GT;
&LT; DIV&GT;
&LT;输入类型=复选框NG模型=选择NG点击=checkAll(选择)/&GT;检查我
&LT; / DIV&GT;
&LT; /手风琴组&gt;
&LT; /手风琴与GT;
&LT; / DIV&GT;
&LT; /身体GT;&LT; / HTML&GT;
的script.js:
VAR应用= angular.module('plunker',['ui.bootstrap']);
app.controller('AccordionDemoCtrl',函数($范围){
$ scope.checkAll =功能(选择){
警报(选择);
};
});
I have used https://github.com/angular-ui/bootstrap for accordion, but with this directive I'm having scope issues. It only allows to use scope if ngController is declared inside of accordion.
Please visit below two Plunker links and you will understand what I'm trying to say:
Example 1: http://plnkr.co/edit/Fb4UauWWmHOnTyjMPFBo
index.html
<!doctype html>
<html ng-app="plunker">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-animate.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.4.js"></script>
<script src="script.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="AccordionDemoCtrl">
<accordion>
<accordion-group is-open="status.open">
<accordion-heading>
Accordian - Click me <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
</accordion-heading>
<div>
<input type="checkbox" ng-model="select" ng-click="checkAll()" /> Check me
</div>
</accordion-group>
</accordion>
</div>
</body>
</html>
script.js
var app = angular.module('plunker', ['ui.bootstrap']);
app.controller('AccordionDemoCtrl', function($scope) {
$scope.checkAll = function() {
alert($scope.select);
};
});
Example 2: http://plnkr.co/edit/ljEMUnTqPBqUyub5eEB7
index.html
<!doctype html>
<html ng-app="plunker">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-animate.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.4.js"></script>
<script src="script.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<accordion>
<accordion-group is-open="status.open">
<accordion-heading>
Accordian - Click me <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
</accordion-heading>
<div ng-controller="AccordionDemoCtrl">
<input type="checkbox" ng-model="select" ng-click="checkAll()" /> Check me
</div>
</accordion-group>
</accordion>
</body>
</html>
script.js
var app = angular.module('plunker', ['ui.bootstrap']);
app.controller('AccordionDemoCtrl', function($scope) {
$scope.checkAll = function() {
alert($scope.select);
};
});
I found the solution:
We can pass the value from the function itself, not necessary to access value using $scope.
Demo: http://plnkr.co/edit/fZZrDN4e8kbvimR2Wzya
index.html
<!doctype html>
<html ng-app="plunker">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-animate.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.4.js"></script>
<script src="script.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="AccordionDemoCtrl">
<accordion>
<accordion-group is-open="status.open">
<accordion-heading>
Accordian - Click me <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
</accordion-heading>
<div>
<input type="checkbox" ng-model="select" ng-click="checkAll(select)" /> Check me
</div>
</accordion-group>
</accordion>
</div>
</body>
</html>
script.js:
var app = angular.module('plunker', ['ui.bootstrap']);
app.controller('AccordionDemoCtrl', function($scope) {
$scope.checkAll = function (select) {
alert(select);
};
});
这篇关于在尝试访问范围问题的角度,引导指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!