角范围混乱 [英] angular scope confusion

查看:225
本文介绍了角范围混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,可以说我写加载一个模板到一个div一个自定义的指令。我的自定义指令被称为 loadClickedTpl 。当 loadClickedTpl 点击它应该读取属性并加载模板到 #target

So lets say I am writing a custom directive that loads a template into a div. My custom directive is called loadClickedTpl. When loadClickedTpl is clicked it should read the attribute and load the template into #target.

到目前为止,我的HTML看起来像这样:

So far my html looks something like this:

 <html np-app="mymodule">
      <head>...</head>
      <body>

           <a loadClickedTpl tplSrc="myTpl" > Click Me to load template </a>

           <div id="target" ng-include src="clickedTpl"></div>

           <script type="text/ng-template" id="myTpl">
                <h1>Loaded</h1>
           </script>

      </body>
 </html>

问题是设置 clickedTpl 变种指向的模板。如果其在html做过类似这样&LT; D​​IV ID =目标NG-包括SRC ='myTpl'&GT;&LT; / DIV&GT; 它工作正常,这样做编程方式被证明是一个挑战。这里是我到目前为止已经试过:

The problem is setting the clickedTpl var to point to the template. If its done in the html like so <div id="target" ng-include src="'myTpl'"></div> it works fine, doing it programmatically is proving to be a challenge. Here is what I have tried so far:

angular.module('loadATpl').directive 'loadClickedTpl', ->
     (scope, element, attrs) ->
           element.bind 'click', -> 
                # does not work
                scope.clickedTpl = attrs.tplSrc

                # also does not work
                angular.injector(['ng']).invoke ($rootScope) ->
                       $rootScope.clickedTpl = attrs.tplSrc

                # obviously does not work
                clickedTpl = atts.tplSrc

angular.module('mymodule', ['loadATpl'])

的点击绑定的工作,但就是被它结束。

The click binding does work, but that is were it ends.

推荐答案

下面是工作示例:<一href=\"http://plnkr.co/edit/8BNYr9J8g6tRLMo8VdPi?p=$p$pview\">http://plnkr.co/edit/8BNYr9J8g6tRLMo8VdPi?p=$p$pview

您需要使用的attrs为负载点击-TPL(复姓前pression)的角
指令。

You need to use attrs as 'load-clicked-tpl' (hyphenated expression) for angular directives.

这篇关于角范围混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆