如何访问模板脚本AngularJS变量 [英] How to access AngularJS variable from template script

查看:140
本文介绍了如何访问模板脚本AngularJS变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器:

$scope.totals = totals;

(作品预期HTML注入)我的模板:

My template (works as expected for html injection):

{{totals}}

但我需要的是访问变量总计在脚本中的模板,像这样:

But what I need is to access the variable totals in a script in the template, like so:

<script>
  var totals = ????;
  // do stuff with totals
</script>

我试过 $ scope.totals $总计 {{总计}} 等,有没有用。我想AP preciate任何见解,谢谢!

I've tried $scope.totals, $totals, {{totals}}, etc, with no avail. I would appreciate any insight, thanks!

编辑:

以下是我的控制器和模板的的jsfiddle。里面我想插入使用 $ scope.totals 变量的脚本的模板。

The following is a jsfiddle of my controller and template. Inside of the template I'm wanting to insert a script that uses the $scope.totals variable.

http://jsfiddle.net/38CrC/

推荐答案

OP答:

我最后不得不用一个指令来找到我想要的价值。在这种设置我想用我添加了一个指令,使其看起来像这样的值时,控制器:

I ended up having to use a directive to find the value I wanted. In the controller that set the value I wanted to use I added a directive so that it looked like this:

'use strict';

angular.module('AdminApp')
  .controller('AdminEventsCtrl', function ($scope, collection) {
    $scope.totals = collection;
  }).directive('foo', function(){
    return {
        restrict: 'A',
        link: function(scope, elem, attrs){
            //Use scope.totals here
        }
    }
});

在我的模板,我有声明:

In my template I had the declaration:

<div foo></div>

这给了我做什么,我需要的变量总数的能力。

This gave me the ability to do what I needed with the variable totals.

特别感谢@jvandemo帮助我在此得出答案。

Special thanks to @jvandemo for helping me arrive at this answer.

这篇关于如何访问模板脚本AngularJS变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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