简体   繁体   English

AngularJs指令 - <script> inside template

[英]AngularJs Directive - <script> inside template

I have a directive with a template and inside this template I have a <script> tag using variables of the directive. 我有一个带模板的指令,在这个模板中我有一个<script>标签,使用该指令的变量。

Directive: 指示:

    (function () {
      'use strict';

      angular
        .module('app.components')
        .directive('picker', Picker);

      /*@ngInject*/
      function Picker() {

        return {
          restrict: 'E',
          controller: PickerController,
          controllerAs: 'vm',
          bindToController: true,
          templateUrl: 'picker.html',
          transclude: true,
          scope:{
            inputId: '@'
          }
        };

        /*@ngInject*/
        function PickerController() {
          /*jshint validthis: true */
          var vm = this;
        }

      }
    })();

Template: 模板:

    <div>
      <div>
        id: {{vm.inputId}}
        <ng-transclude></ng-transclude>
      </div>
      <script>
        console.log({{vm.inputId}});
      </script>
    </div>

Usage: 用法:

    <picker input-id="myInput"> <!-- something... --> </picker>

The problem is that the {{vm.inputId}} inside the <script> tag isn't filtered so {{vm.inputId}} doesnt become "myInput". 问题是<script>标记内的{{vm.inputId}}未被过滤,因此{{vm.inputId}}不会成为“myInput”。 Everything works outside the <script> tag, id: {{vm.inputId}} becomes id: myInput 一切都在<script>标记之外, id: {{vm.inputId}}变为id: myInput

Is it just not possible to put "variables" inside a script tag? 是不是可以将“变量”放在脚本标记中?

The piece of code that is implemented in 实现的代码片段

<script>
      console.log({{vm.inputId}});
</script>

can be well implemented inside your directive's controller. 可以在指令的控制器中很好地实现。 That will allow you to run the javascript code with the luxury of having access to your variables. 这将允许您运行javascript代码,可以访问您的变量。

For example you can have this: 例如,你可以这样:

var app =  angular.module('myApp', [])

app.directive('testDirective', function(){
  return {
    restrict: 'E',
    template: '<p>Click in the text box</p>'+
              '<textarea id="my-area"></textarea>'+
              '<p>Click {{num_clicks}}</p>',
    controller: function($scope, $log){
      $scope.num_clicks = 0;
      $("#my-area").click(function(){
        incr();
      });
      function incr(){
        $scope.num_clicks = $scope.num_clicks + 1;
        $scope.$digest();
        $log.log("A click",   $scope.num_clicks);
      }

    }
  };
});

I hope this helps 我希望这有帮助

I would not recommend using a <script> tag inside your template at all. 我不建议在模板中使用<script>标签。

If you want to log the value of inputId at the moment the view is loaded then you could use the ngInit directive instead. 如果要在加载视图时记录inputId的值,则可以使用ngInit指令。

<div ng-init="log(vm.inputId)">
    id: {{vm.inputId}}
    <ng-transclude></ng-transclude>
</div>

and add the log function to your scope in the controller: 并将日志功能添加到控制器中的范围:

app.controller('myController', function($scope, $log) {
    $scope.log = function (message) {
        $log.log(message)
    };
});

Well, jQlite does not support script tags in templates . 好吧, jQlite不支持模板中的脚本标记 jQuery does, so the recommendation is to include jQuery if you need this functionality. jQuery可以,所以如果你需要这个功能,建议包括jQuery。

Further, 进一步,

Even if you use the <script> tag in your template, the code within would be executed outside angular's context. 即使您在模板中使用<script>标记,其中的代码也会 angular的上下文之外执行。 So you would not be able to access any variable inside controller's scope in your <script> tag within the template file. 因此,您将无法访问模板文件中<script>标记内控制器范围内的任何变量。 This essentially means, doing something like 这基本上意味着,做类似的事情

  <script>
    console.log({{vm.inputId}});
  </script>

is not possible, because neighter vm, nor inputId would be available and you would infact get an error claiming Unexpected token {{ 是不可能的,因为更新的vm,也没有inputId可用,你会得到一个错误声称Unexpected token {{

Again, you could have written the same code in controller anyways. 同样,你可以在控制器中编写相同的代码。 So why complicate things 那么为什么要复杂化

If you still intend to use the <script> within your template, here's a plunkr 如果您仍打算在模板中使用<script> ,那么这里有一个plunkr

just include the library script with the rest of your scripts (angular etc) on your index. 只需在您的索引中包含库脚本和其余脚本(角度等)。

You can still wrap the datepicker in a directive -- use the link function of the directive. 您仍然可以将datepicker包装在指令中 - 使用指令的link函数。 if jQuery is in your project you will have access to all jquery functions on the "element" parameter of your link function. 如果jQuery在您的项目中,您将可以访问链接函数的“element”参数上的所有jquery函数。

angular.module('myModule').directive('datepicker', function () {
  return {
    link: function (scope, elem, attrs) {
      elem.jqdatepicker({ /* options */ });
    }
  };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM