我想知道是否有一种角度有条件地显示内容而不是使用ng-show等.例如在backbone.js我可以在模板中使用内联内容做一些事情,如:
<% if (myVar === "two") { %> show this<% } %>
Run Code Online (Sandbox Code Playgroud)
但在角度方面,我似乎仅限于显示和隐藏包含在html标签中的内容
<p ng-hide="true">I'm hidden</p>
<p ng-show="true">I'm shown</p>
Run Code Online (Sandbox Code Playgroud)
如果使用{{}}而不是将内容包装在html标记中,有条件地以角度显示和隐藏内嵌内容的角度建议方法是什么?
我有一个程序,将变量分配为:
var theList = document.getElementById('theList');
Run Code Online (Sandbox Code Playgroud)
它使用jquery,但如果我这样写:
var theList = $('#theList');
Run Code Online (Sandbox Code Playgroud)
使用该变量的函数不起作用.
jquery选择器和使用getElementById之间有什么区别?
我正在整理一个骨干示例,其中创建了模型并将其删除.我能够将新模型和编辑保存到本地存储,但是在刷新时正确显示localstorage时遇到问题.它似乎是作为单个对象加载,因此无论添加了多少个,都会给我一个模型.
var Thing = Backbone.Model.extend({
defaults: {
title: 'blank'
}
});
var ThingView = Backbone.View.extend({
template: _.template('<b><button id="remove">X</button> <b><button id="edit">Edit</button> <%= title %></b>'),
editTemplate: _.template('<input class="name" value="<%= name %>" /><button id="save">Save</button>'),
events: {
"click #remove": "deleteItem",
"click #edit": "editItem",
"click #save": "saveItem",
},
deleteItem: function () {
console.log('deleted');
this.model.destroy();
this.remove();
},
editItem: function () {
console.log('editing');
this.$el.html(this.editTemplate(this.model.toJSON()));
},
saveItem: function () {
console.log('saved');
editTitle = $('input.name').val();
console.log(editTitle);
this.model.save({
title: editTitle
});
this.$el.html(this.template(this.model.toJSON()));
},
render: function () {
var attributes = …Run Code Online (Sandbox Code Playgroud) 以下命令在控制台中返回错误"ReferenceError:未定义ThingCtrl"
var myApp = angular.module('myApp', []);
myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/things', {templateUrl: 'partial.html', controller: ThingCtrl}).
when('/things/:id', {templateUrl: 'detail.html', controller: ThingCtrl}).
otherwise({redirectTo: '/things'});
}]);
myApp.controller('ThingCtrl', ['$scope', '$routeParams', function($scope, $routeParams) {
$scope.thing = [
{
'title':'first thing',
'first':'one',
'second': 'two',
'third': 'three'
}
];
}]);
Run Code Online (Sandbox Code Playgroud)
但是当控制器被定义为:
function ThingCtrl($scope, $routeParams) {
$scope.thing = [
{
'title':'first thing',
'first':'one',
'second': 'two',
'third': 'three'
}
]
};
Run Code Online (Sandbox Code Playgroud)
为什么使用模块化语法不起作用?
我有一个基本的backbone.js应用程序,它呈现了一组模型.我想修改为仅渲染最后一个模型,并显示模型总数的数字.到目前为止,这是我的代码:
var Thing = Backbone.Model.extend({
});
var ThingView = Backbone.View.extend({
el: $('body'),
template: _.template('<h3><%= title %></h3>'),
render: function(){
var attributes = this.model.toJSON();
this.$el.append(this.template(attributes));
}
});
var ThingsList = Backbone.Collection.extend({
model: Thing
});
var things = [
{ title: "Macbook Air", price: 799 },
{ title: "Macbook Pro", price: 999 },
{ title: "The new iPad", price: 399 },
{ title: "Magic Mouse", price: 50 },
{ title: "Cinema Display", price: 799 }
];
var thingsList = new ThingsList(things);
var ThingsListView …Run Code Online (Sandbox Code Playgroud)