奇怪的角度绑定问题

时间:2014-10-18 19:49:15

标签: angularjs

我有一个奇怪的angularjs绑定问题

<a class="navbar-brand" href="#">Application (Environment: {{ globalConfig.environment + ""}})</a>

这将有效,并呈现值“dev”

<a class="navbar-brand" href="#">Application (Environment: {{ globalConfig.environment }})</a>

这不会呈现任何内容。

<a class="navbar-brand" href="#">Application (Environment: <span ng-bind="globalConfig.environment"></span>)</a>

最后一个例子有效。

为什么第二个例子不会呈现值“dev”的任何想法?

控制器如下:

angular.module('uppApp').controller('globalController', function ($scope) {
    $scope.globalConfig = {
        environment: 'dev'
    };
});

html如下所示:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" ng-app="uppApp">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Pixel Pimp Web UI</title>

    <link href="assets/css/style.css" rel="stylesheet" />

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="assets/thirdparty/ie8mediasupport.js"></script>
    <![endif]-->

</head>
<body ng-controller="globalController">
    <div class="navbar navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Pixel Pimp (Environment: <span ng-bind="globalConfig.environment"></span>)</a>
            </div>
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#">Dashboard</a></li>
                    <li><a href="#about">Pixel Groups</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </div>

    <div class="container">
        <h1>Dashboard</h1>

        <div id="myfirstchart" style="height: 250px;"></div>
    </div><!-- /.container -->


    <script src="assets/thirdparty/built.js"></script>
    <script src="assets/js/uppangular.js"></script>

    <script>
    $(function () { genGraph(); });
    function genGraph() {
        new Morris.Line({
            // ID of the element in which to draw the chart.
            element: 'myfirstchart',
            // Chart data records -- each entry in this array corresponds to a point on
            // the chart.
            data: [
              { year: '2008', value: 20 },
              { year: '2009', value: 10 },
              { year: '2010', value: 5 },
              { year: '2011', value: 5 },
              { year: '2012', value: 20 }
            ],
            // The name of the data record attribute that contains x-values.
            xkey: 'year',
            // A list of names of data record attributes that contain y-values.
            ykeys: ['value'],
            // Labels for the ykeys -- will be displayed when you hover over the
            // chart.
            labels: ['Value']
        });
    }
    </script>

</body>
</html>

更新

当我包装装饰插值服务以进行调试时:

app.config(function($provide){
    $provide.decorator("$interpolate", function($delegate){

        var interpolateWrap = function(){
            var interpolationFn = $delegate.apply(this, arguments);
            if(interpolationFn) {
                return interpolationFnWrap(interpolationFn, arguments);
            }
        };

        var interpolationFnWrap = function(interpolationFn, interpolationArgs){
            return function(){
                var result = interpolationFn.apply(this, arguments);
                var log = result ? console.log : console.warn;
                log.call(console, "interpolation of  " + interpolationArgs[0].trim(), 
                                  ":", result.trim());
                return result;
            };
        };

        angular.extend(interpolateWrap, $delegate);
        return interpolateWrap;

    });
});

控制台日志将显示:

interpolation of  {{globalConfig.environment+""}} : dev 

然而,对于{{globalConfig.environment}}的绑定,控制台窗口中没有显示任何内容。

1 个答案:

答案 0 :(得分:1)

好的我明白了。我第一次使用grunt烘焙,我认为grunt烘焙,是从渲染文件中删除{{globalConfig.environment}}代码。

对不起。愚蠢的错误。

更新

最终我通过修改BAKE解析模式来修复它以使用[{}]代替{{}}

bake: {
        build: {
            options: {
                parsePattern: '/\[\{\s?([\.\-\w]*)\s?\}\]/g'
            },
            files: {
                "app/index.html": "app/base.html"
            }
        }
    },