为什么AnguarJS数据绑定不起作用

Why is this AngualrJS data binding not working?

本文关键字:不起作用 数据绑定 AnguarJS 为什么      更新时间:2023-09-26

如何创建输出HTML的angularjs过滤器,它看起来像我想要的,但我的版本还不起作用。在我的HTML中,我有:

    <ul>
        <li ng-repeat="book in books | filter:query">
            {{book.title}}
            <div ng-bind-html="book.snippet"></div>
        </li>
    </ul>

但它只显示书名,就好像<div ng绑定html>不在那里。控制器具有所有所需的数据,从开始

$scope.books = [
        {
        'image': '/images/the_best_of_jonathans_corner_full.jpg',
        'snippet': '<p>If you read just one book from this site, <em>The Best of Jonathan''s Corner</em> is a head taller than the others. It contains   all of the best works of theology from Jonathan''s Corner, and there''s a lot to dig through&mdash;but only if you want. If not, feel free to enjoy and read  as little or as much as you like.</p><p>This book is the author''s favorite title out of all the books sold from this site.</p>',
        'title': 'The Best of Jonathan''s Corner',
        'url': '/redirect/the_best_of_jonathans_corner.html'
        }, 

网页看起来像:

CJS Hayward
Search books:
    The Best of Jonathan's Corner
    Doxology: The Anthology
    The Luddite's Guide to Technology 

为什么出现了书的标题,却没有出现小册子?我在一些片段中有HTML,我希望它们将片段呈现为HTML。(我可以有一个转义版本为{{book.sippet}},但我想知道在这里处理HTML(非)转义的正确方法。)所有的代码片段都是作为严格有效的XHTML编写的,并以<p>标签等

该项目旨在成为AngularJS phonecat教程的一部分。

--更新--

谢谢你,@ppa;我已经编辑了源文件,包括:

    <script src="/js/angular.min.js"></script>
    <script src="/js/angular-sanitize.js"></script>
    <script src="/js/controllers.js"></script>

controllers.js现在开始:

'use strict';
var authorApp = angular.module('authorApp', ['ngSanitize']);

AngularJS没有重复任何内容(我在console.log中没有看到任何新内容):

CJS Hayward
Search books:
    {{book.title}}

我还应该包括什么?该应用程序当前位于http://new.cjsh.name.

在开发者工具中简要查看了您的代码后,我可以看到您使用的是angular 1.3版(beta5)的angular-splicate.js。但您的angular.js脚本是1.2版。我猜问题出在这个版本不匹配上。请尝试使用相同1.3版本的angular.js脚本,看看这是否有效。你可以在这里找到它。

要使ng-bind-html指令正常工作,必须提供$sance服务。你的应用程序中包含ngSanitize模块和ng-sanise.js脚本了吗?