【发布时间】:2014-02-17 23:31:57
【问题描述】:
我有以下主视图
<div ng-include="'otions.html'"></div>
options.html 有以下内容
<div ng-controller="OptionsController">Options</div>
<input type="text" ng-model="keyword" value="{{keyword}}" />
<button ng-click="search()">Search</button>
但是“关键字”没有绑定到 OptionsController 中的范围。
app.controller('OptionsController',
['$scope', function($scope) {
$scope.keyword = "all";
$scope.search = function() {
console.log("hello")
};
}]);
当我点击按钮时,我没有看到 hello 并且关键字 all 没有出现在输入文本中。
我尝试如下移动 ng-controller 部分
<div ng-controller="OptionsController" ng-include="'otions.html'"></div>
一切都按预期进行。
我阅读了AngularJS - losing scope when using ng-include 中的答案 - 我认为我的问题是相关的,但只需要更多解释来了解发生了什么。
【问题讨论】:
-
不确定这里是否是错字,但您的来源中也有:您所包含的文件引用为
otions.html而不是options.html -
在您的源代码中,您仅为第一个
div指定 ng-controller,因此您的输入和按钮不在控制器的范围内。可能这是一个错字。如果不是,那可能是一个原因。
标签: javascript angularjs