数据不会在视图中绑定

时间:2016-01-10 02:30:26

标签: javascript angularjs

在下面的代码中,

<!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Controller</title>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.20/angular.js"></script>
            <script type="text/javascript">
                var app = angular.module("sample", []);
                app.controller("emp", function(){
                        this.Name = "jag";
                    });
            </script>
        </head>
        <body ng-app="sample">
            <div ng-controller="emp">
                Hello {{Name}}
            </div>

        </body>
    </html>

{{ Name }}表达式未评估。

1)如何检查$scope模型是否与视图中的div模板绑定?

2)我了解到范围位于DOM。使用指令ng-controller="emp"创建控制器实例。我可以通过DOM访问控制器(emp)实例和$scope吗?

1 个答案:

答案 0 :(得分:2)

  

如果您使用controller as而不是this,请使用$scopeControllers classesinitializingvar student = new Student()他们(controller as)使用var app = angular.module("sample", []); app.controller("emp", function() { this.Name = "jag"; });语法。

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="sample">
  <div ng-controller="emp as e">
    Hello {{e.Name}}
  </div>

</body>
&#13;
function checkForKey(key, json) {
    var found = false;
    for(var x in json) {
        if (json.hasOwnProperty(x)) {
            if(json[x][data] == key) {
                found = key;
            }
        }
    }
    return found;
}
&#13;
&#13;
&#13;