Angular Bind不起作用

时间:2016-03-15 22:26:51

标签: angularjs angularjs-directive

我正在尝试构建一个表指令,但是当我尝试设置一些绑定到我的模板时,它不起作用。

这是我的$ scope.grid对象。

$scope.grid = {
    title: "My Title",
    isCheckable: true,
    toolbar: [{ text: "", icon: "", hint: "", action: "" }],
    dataSource: "ok",
    dataBind: [{ text: "", bind: "", type: "" }],
    defaultFilter: [{ bind: "" }],
    actions: [{ text: "", icon: "", hint: "", action: "" }]
};

如果我尝试

$scope.title = $scope.grid.title;
<div>{{title}}</div>

它工作并向我显示标题&#34;我的标题&#34;

但如果我尝试

<div>{{grid.title}}</div>

它没有用。

*解决方案*

删除并重新创建虚拟目录。 出于某种原因,它使用旧版本锁定了我的模板,因此,在我的模板中,它显示在html&#34; {{title}}&#34;而不是&#34; {{grid.title}}&#34;

1 个答案:

答案 0 :(得分:0)

不要忘记添加ng-app和ng-controller,请看这个例子:

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">                </script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

{{grid.title}}

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.grid = {
    title: "My Title",
    isCheckable: true,
    toolbar: [{ text: "", icon: "", hint: "", action: "" }],
    dataSource: "ok",
    dataBind: [{ text: "", bind: "", type: "" }],
    defaultFilter: [{ bind: "" }],
    actions: [{ text: "", icon: "", hint: "", action: "" }]
};
});
</script>

</body>
</html>