我有代码在使用AJAX请求时完美地从数组中读取数据.但是,当我将对象推送到数组时,ng-repeat不会呈现新行,我必须刷新页面然后获取发送到服务器的数据.
为什么这样做?谢谢
function processError() {
var responseCode = 404;
var error = {};
error["client"] = document.getElementById('client').value;
error["errorMessage"] = document.getElementById('error-message').value;
error["simpleRes"] = document.getElementById('simple-fix').value;
error["fullRes"] = document.getElementById('full-fix').value;
error["reason"] = document.getElementById('reason').value;
var errorJson = JSON.stringify(error);
$.ajax({
url: "../ErrorChecker/rest/error",
type: "POST",
data: errorJson,
contentType: "application/json"
})
.done(function (data, statusText, xhr, displayMessage) {
$('.form').hide();
responseCode = xhr.status;
reloadData(data);
});
function reloadData(data) {
if (responseCode == 200) {
processPositiveResponse(data);
} else {
$('#negative-message').show(1000);
}
}
}
function processPositiveResponse(data) {
$('#positive-message').show(1000);
updateTable(data);
$('#errorTable').DataTable().destroy();
setupTable();
clearInputs();
console.log($scope.controller.errors); …Run Code Online (Sandbox Code Playgroud) 我有一个新项目,我必须使用ember.我怎样才能过渡到余烬,因为我对角度有很强的理解和经验.
我们使用JQueryajax读取json数据并转换为对象.
jQuery脚本
$.ajax({
type: "GET",
dataType: 'json',
url: 'data.json',
converters:
{
"text json": function (data) {
return $.parseJSON(data);
}
},
success: function (data) {
self.jsonData = data;
}
});
Run Code Online (Sandbox Code Playgroud)
data.json
[{
"Name" : "Task 1",
"ID" : 1,
"StartTime" : "2014-02-02T00:00:00Z",
"Effort" : "8:00:00",
"Description" : "Description of Task 1"
},
{
"Name" : "Task 2",
"ID" : 2,
"PredecessorIndices" : "1",
"StartTime" : "2014-02-03T00:00:00Z",
"Effort" : "16:00:00",
"Description" : "Description of Task 2"
},
{ …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
<input id="id">
<button data-action="bea" ng-click="Create($('#id1')[0].value);" class="btn">Insert ID</button>
<button data-action="bea" ng-click="Create($('#id2')[0].value);" class="btn">Insert ID</button>
Run Code Online (Sandbox Code Playgroud)
在JS我有:
$scope.Create = function (id){
if (id === undefined) {
$scope.data = "You must specify an id";
} else {
$scope.data = data;
console.log(data);
});
}
};
Run Code Online (Sandbox Code Playgroud)
当调用进入Create函数时,id的值是未定义的.
如果我在Create函数的beginging中添加以下行,一切正常:
id = $('#id')[0].value;
Run Code Online (Sandbox Code Playgroud)
如果我发送一个常量值,它可以工作:
<button data-action="bea" ng-click="Create('SomeID');" class="btn">Insert ID</button>
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况,如何在不将价值线放入方法的情况下做到这一点?
谢谢
html javascript angularjs angularjs-scope angularjs-ng-click
我想将角度事件和模型绑定到javascript代码附加的html元素.
我的代码在这里.https://jsfiddle.net/hq7qk48n/13/
<div ng-app>
<div ng-controller="MyController">
<input type="text" ng-model="text1">
<a href="#" ng-click="onClick()">click1</a>
<div id="append"></div>
<p ng-if="clickedTime1">click1 : {{ clickedTime1.toLocaleString() }}</p>
<p ng-if="clickedTime2">click2 : {{ clickedTime2.toLocaleString() }}</p>
<p>{{ text1 }}</p>
<p>{{ text2 }}</p>
</div>
</div>
function MyController($scope) {
$scope.clickedTime1 = null;
$scope.clickedTime2 = null;
$scope.onClick = function () {
var html = '<input type="text" ng-model="text2" name="text"> <a href="#" ng-click="onClick2()">click2</a>';
$("#append").empty();
$("#append").append(html);
$scope.clickedTime1 = new Date();
}
$scope.onClick2 = function () {
$scope.clickedTime2 = new Date();
};
}
Run Code Online (Sandbox Code Playgroud)
onClick2()不起作用.并且模型"text2"未更新.如何绑定onClick2函数和text2模型?
需要编译一个html元素?怎么样?
这是Angular中的错误还是我遗漏了一些东西:
从指令的模板调用函数表明该函数执行了11次!用templateurl,22次!! 用字符串模板.
angular.module('testDirective', [])
.directive('myDirective', function() {
return {
scope:{},
template: '{{increment()}} {{count}}',
controller: function($scope) {
$scope.count = 0;
$scope.increment = function() {
$scope.count += 1;
};
}
};
})
Run Code Online (Sandbox Code Playgroud)
HTML:
<body ng-app="testDirective">
<my-directive></my-directive>
</body>
Run Code Online (Sandbox Code Playgroud)
结果:
22
Run Code Online (Sandbox Code Playgroud)
这是一个包含template和templateUrl方法的Plunker.
例如,当在转发器中涉及方法调用时,这是一个非常严重的问题,这最终会在相同的方法上调用相同的方法.
有人可以对此有所了解吗?
我正在研究 angular 教程项目 ---- angular-phonecat,我到了第 5 步。
出于好奇,我用 jquery ajax 方法替换了原始的 angular ajax 方法,其余部分保持不变。之后我发现我可以从服务器获取正确的数据,但数据绑定永远不会起作用。
这是我的代码:
'use strict';
/* Controllers */
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', ['$scope', '$http', function ($scope, $http) {
//$http.get('phones/phones.json').success(function(data) {
// $scope.phones = data;
//});
$.ajax({
type: "GET",
url: "phones/phones.json",
contentType: "application/json",
global: false,
success: function (data) {
$scope.phones = data;
}
});
$scope.orderProp = 'age';
}]);
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况?我错过了什么重要的事情吗?
我想将css类设置为列表中的项目,具体取决于子元素是否符合某个标准.结构类似于以下示例:
<ul ng-controller="Navigation">
<li><a href="#">Category A</a>
<ul>
<li><a href="a1.html">a1</a></li>
<li><a href="a2.html">a2</a></li>
</ul>
</li>
<li><a href="#">Category B</a>
<ul>
<li><a href="b1.html">b1</a></li>
<li><a href="b2.html">b2</a></li>
</ul>
</li>
<li><a href="contact.html">contact</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我的模型是当前的页面a2.html.如果链接具有与href模型值相同的属性,则它应具有某个css类(active).这可以通过以下表达式完成:
<a href="a1.html" ng-class="{'active': currentPage == 'a1.html'}>
Run Code Online (Sandbox Code Playgroud)
但这有点不优雅,因为我必须重复文件名(a1.html).是否可以将当前元素传递给函数?像这样的东西:ng-class="getClass(currentElement)"
接下来的问题是,我想根据孩子是否有班级来选择父元素active.如果<a href="a1.html">a1</a>在上面的例子中被选中,那么也Category A应该得到这个类active.
结论
'stewie的解决方案有效,但我得出结论,Angular不是这项工作的最佳工具.它不是一个web'应用程序'(Angular的域),而是静态html,应该稍微丰富一下.
这个简单的jQuery代码段完成了这项工作:
var activeLink = $("a").filter(function() {
return $(this).attr("href") == currentPage();
});
activeLink.addClass("active");
activeLink.parents("li").children("a").addClass("active");
Run Code Online (Sandbox Code Playgroud) 我是核心C++开发人员,最近我学习了Scala和Lift并学习了AngularJS.我需要开发一个类似于http://www.jotform.com/的应用程序.
有很多机会可以更改现有功能或为我的应用程序添加新功能.我想根据http://www.slideshare.net/ericshepherd/building-a-javascript-module-framework-at-gilt构建具有插件架构支持的应用程序.如何使用AngularJS开发插件架构.由于我对网络开发很陌生,我不确定我是否正朝着正确的方向前进.
请告诉我哪个是开发应用程序的最佳方式?哪个更适合(AngularJS/JQuery)用于开发像jotform这样的应用程序?
我多年来一直在使用jQuery.现在,我想学习构建单页网站的新内容.我选择了AngularJS.
目前我想知道在AngularJS中使用jQuery是否是一个好主意?
例.
我在AngularJS控制器中有类似的东西:
$http.post('ajax/gettimeline', { page: page }).success(function (data) {
$rootScope.loading = false;
// Here I need to built dynamic html with getting and setting
// dynamic elements size, positions, change their properties, etc.
}
});
Run Code Online (Sandbox Code Playgroud)
使用jQuery构建这些动态html元素是一项简单的任务.但是使用AngularJS看起来会很痛苦.
将不胜感激任何建议.
谢谢
我尝试了许多不同的东西来试图让它发挥作用.我读过了:
当onclick执行时,ng-click不会在AngularJS中触发
还有更多
HTML:
<div ng-controller="testApp">
<div id="bla">
<button ng-click="obey('bla')">Close</button>
<h4>Bla bla bla</h4>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
var testApp = angular.module('testApp', []);
testApp.controller('testController', function($scope) {
$scope.obey = function test(id) {
$("#" + id).fadeOut("slow", function() {
this.remove()
});
};
});
Run Code Online (Sandbox Code Playgroud)
由于某种原因,div根本不会淡出.