Javascript 初学者在Angularjs中的范围问题
我正在使用angular作为前端,django作为后端,编写smll来学习angular。我试图显示我在范围中定义的变量。我正在加载html作为锚标记的部分onclick。 以下是我的app.js,其中包含我的路由器设置:Javascript 初学者在Angularjs中的范围问题,javascript,django,angularjs,Javascript,Django,Angularjs,我正在使用angular作为前端,django作为后端,编写smll来学习angular。我试图显示我在范围中定义的变量。我正在加载html作为锚标记的部分onclick。 以下是我的app.js,其中包含我的路由器设置: var blogApp = angular.module('blogApp',['ngRoute']) blogApp.config(function($interpolateProvider) { $interpolateProvider.startSymbol(
var blogApp = angular.module('blogApp',['ngRoute'])
blogApp.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{$');
$interpolateProvider.endSymbol('$}');
});
blogApp.config(['$routeProvider',function($routeProvider){
$routeProvider.
when('/login', {
templateUrl: 'static/partials/login.html',
}).
when('/register',{
templateUrl:'static/partials/register.html',
}).
when('/shoppingcart', {
templateUrl:'static/partials/shopping_cart.html',
controller:'CartController'
}).
otherwise({
redirectTo: '/'
});
}]);
以下是我的CartController定义:
blogApp.controller('CartController', function($scope){
$scope.items = [
{title: 'Paint pots', quantity: 8, price: 3.95},
{title: 'Polka dots', quantity: 17, price: 12.95},
{title: 'Pebbles', quantity: 5, price: 6.95}
];
});
我已经包括了包含我的CartController和定义我的路由的js。我的部分(称为shopping_cart.html)如下所示:
<div ng-controller='CartController'>
<h3>Your Cart</h3>
<div ng-repeat="item in items">
<span>{% item.title %}</span>
<input ng-model="item.quantity">
<span> {% item.price|currency %} </span>
<span> {% item.price * item.quantity | currency %} </span>
<button ng-click="remove($index)">Remove</button>
</div>
</div>
你的车
{%item.title%}
{%item.price | currency%}
{%item.price*item.quantity | currency%}
去除
有趣的问题是item.quantity显示正确,但item.title、item.price等显示在我的输出中。关键是它是否显示item.quantity这意味着item变量包含定义的变量,即使我将item.title放在input中代替item.quantity,它也会显示变量,但{%item.title%}请指出我的错误,并请解释为什么会发生这种情况 看起来像是打字错误
在您的配置中,您覆盖了插入到“{$”和“$}”的开始和结束符号,但在您的模板中,您使用了“{%”和“%}”。将%替换为$,它应该可以工作 是的,谢谢。我以前有这样的习惯,因为它在django是有效的。谢谢你细心的眼睛。:)