Javascript 从指令访问AngularJS范围
我正在尝试访问app controller中定义的范围变量,以便根据我创建的指令更新单击事件,但它似乎无法从其隔离范围访问全局范围 范围变量已定义(currentTitle): 指令中定义的范围为:Javascript 从指令访问AngularJS范围,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,我正在尝试访问app controller中定义的范围变量,以便根据我创建的指令更新单击事件,但它似乎无法从其隔离范围访问全局范围 范围变量已定义(currentTitle): 指令中定义的范围为: scope: { //bind the showDetails into the scope so it can be seen by all of the accordion elements promise: '&?', currentTitle: '=?', showD
scope: {
//bind the showDetails into the scope so it can be seen by all of the accordion elements
promise: '&?',
currentTitle: '=?',
showDetails: '=?bind'
}
下面是一个包含所有代码的代码笔:
我想做的是,当单击其中一个标题时,更新标题标题,因为现在我只是尝试硬编码作用域的currentTitle
变量和$apply
——在指令的click事件中,但我没有看到它反映在标题上
谢谢大家! 如果传入带有“&”语法的函数,应该能够在指令中调用它 假设控制器中的函数如下所示:
$scope.someFunction = function(someParameter) {...}
...
scope {
someFunction: "&"
}
...
//in link or controller
$scope.someFunction({someParameter:'foo'});
那么您的指令将如下所示:
$scope.someFunction = function(someParameter) {...}
...
scope {
someFunction: "&"
}
...
//in link or controller
$scope.someFunction({someParameter:'foo'});
...
范围{
someFunction:“&”
}
...
//在链路或控制器中
$scope.someFunction({someParameter:'foo'});
尝试将指令范围上的
currentTitle
键设置为此。它将创建一个双向绑定,允许您在指令范围内轻松更改父控制器中的currentTitle
JS
HTML
{{文章标题}
{{文章标题}
{{article.content}
谢谢!我已将其更改为currentTitle:'&',
,但它也不起作用,可能是因为它不是一个函数而是一个变量?是的,父作用域中的函数将接受传递给函数的变量,并在其自己的作用域上进行更改。或者,$emit发出一个事件。或者不要使用隔离作用域。谢谢!,我曾尝试将currentTitle:设置为“=”,但我完全没有找到HTML标记中的定义来完成双向绑定,干杯!
<accordion promise="promise" current-title="currentTitle">
<!--<dt ng-repeat-start="article in articles" ng-class="showDetails ? 'show-titles' : 'show-titles-not'">.-->
<dt ng-repeat-start="article in articles" ng-class="{'show-titles' : showDetails}">
<div class="grid-30">
<div class="round-div"></div>
</div>
<div class="grid-70">
<h5>{{article.title}}</h5>
</div>
</dt>
<dd ng-repeat-end="" ng-class="{'show-titles' : showDetails}">
<div class="grid-40">
<img src={{article.image}} alt="">
</div>
<div class="grid-60">
<div class="news-desc">
<h5>
{{article.title}}
</h5>
<p>
{{article.content}}
</p>
</div>
</div>
</dd>
</accordion>