AngularJS:输入表单在分部中不起作用

AngularJS: Input form does not work inside the partial

本文关键字:不起作用 输入 表单 AngularJS      更新时间:2024-03-03

页面的层次结构如下:

<div ng-include="'partials/navbar.html'"></div>
<div ng-view></div>
<div ng-include="'partials/footer.html'" id="footer"></div>

这是主index.html,它还包含所有脚本源。

在ng视图中,还有另一个使用头部分的索引:

div ng-include="'partials/header_search.html'"></div>

此header_seatch部分包含基于角度的输入表单

<form name="searchLocation" ng-submit="search()">
        <div class="input-group">
        <input type="text" name="location" ng-model="location" ng-autocomplete ng-change="error = false"/>
        <div class="input-group-addon">
            <button type="submit">Search</button>
        </div>
        </div>
        <span ng-if="error">No such address</span>
    </form>

我的问题来了——下面的表单放在部分头中时没有响应。当我把它复制/粘贴到索引视图中时,一切都很好。angular和放在分部中的ng有任何混淆吗?我应该修改什么以使其在标头部分中工作?

提前感谢!

ng-include有自己的作用域,这非常令人困惑。我建议用指令替换ng include。

否则,这应该有效:

<form name="searchLocation" ng-submit="$parent.search()">
 <div class="input-group">
 <input type="text" name="location" ng-model="$parent.location" ng-autocomplete   ng-change="error = false"/>
    <div class="input-group-addon">
        <button type="submit">Search</button>
    </div>
    </div>
    <span ng-if="$parent.error">No such address</span>
</form>

请注意,使用$parent是不好的做法,应该避免。