我有以下代码:
<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
我有一系列我正在重复的项目.
<li ng-repeat="lineItem in lineItems" class="bouncy-slide-left">
<div class="form-group col-sm-5 col-lg-2 col-xl-2 pad-right">
<label for="expenses">{{lineItem.labels.name}}Expense:</label>
<br>
<select name="expenses" ng-model="expense.name" class="form-control" style="width: 175px;">
<option value="{{expense.name}}" ng-repeat="expense in expenses">{{expense.name}}</option>
</select>
</div>
<div class="form-group col-sm-5 col-lg-2 col-xl-2 pad-right">
<label>Material Cost:</label>
<br>
<input type="text" ng-model="expense.cost" class="form-control" name="material" placeholder="5.00">
</div>
<div class="form-group col-sm-5 col-lg-2 col-xl-2 pad-right">
<label>Quantity:</label>
<br>
<input type="text" ng-model="expense.quantity" class="form-control" name="quantity" placeholder="5">
</div>
<div class="form-group col-sm-5 col-lg-2 col-xl-2 pad-right">
<label>Labor Rate:</label>
<br>
<input type="text" ng-model="expense.labor" class="form-control" name="labor" placeholder="20.00">
</div>
<div class="form-group col-sm-5 col-lg-2 col-xl-2 …Run Code Online (Sandbox Code Playgroud) 如何在不从控制器调用函数的情况下更改范围变量.
我想在editState变量等于1时显示div内容,但它不起作用.
HTML
<body data-ng-controller="profileCtrl as pctrl">
<div data-ng-click="pctrl.editState === 1">Edit</div>
<div data-ng-if="pctrl.editState === 1">
.....
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
JS(在profileCtrl控制器中)
this.editState = 0;
Run Code Online (Sandbox Code Playgroud)
但是当我调用一个函数时它正在工作(我不想这样做)
<div data-ng-click="pctrl.editFn()">Edit</div>
this.editFn = function() {
this.editState = 1;
}
Run Code Online (Sandbox Code Playgroud) javascript operators angularjs angularjs-ng-click angular-ng-if
我是angularJS的新手.在我尝试的时候ng-repeat,我在代码中发现了一个问题(如下所示)
<ul class="dropdown-menu" role="menu" ng-init="names=findDomains()">
<li><a style="cursor:pointer" ng-repeat="x in names" ng-click="selectDomain({{ x }})">{{ x }}</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我的想法是,我的页面上有一个下拉菜单(例如名称为"test").单击时,将显示选择,选项将显示为内容<li></li>.所有选项都按函数返回findDomains()并初始化ng-init.
当选择特定选项(内容<li></li>)时(例如,名称为"opt1"),下拉菜单的文本将使用选项名称更新("opt1"替换"test").这是通过功能实现的selectDomain()
由于显示和调用相同的内容selectDomain(),我放了两个{{x}}调用ng-repeat,希望显示相同的选项调用selectDomain().
然而,一切似乎工作正常(findDomains(),ng-repeat而第二{{x}}外<a></a>).但{{x}}内部<a></a>不能正常工作.单击选项时,下拉菜单名称不会更新.
但是selectDomain()功能很好,因为它可以用纯文本(例如ng-click="selectDomain('opt1'))工作.
任何指导?
我试图根据单选按钮选择显示和隐藏值.我在单击单选按钮时执行此操作.它不适合我
这是HTML和Angular js代码
**HTML Code:**
<div ng-app="ssbApp" ng-controller="ssbCtrl">
<input type="radio" name="showHideExample" ng-model="showHideExample" value="single" ng-click="showHideTest=true">Show
<input type="radio" name="showHideExample" ng-model="showHideExample" value="multi" ng-click="showHideTest=false">hide
<div ng-snow="showHideTest" ng-model="showHideTest">Test hide and show</div>
</div>
**Angular JS Code:**
var ssbApp = angular.module("ssbApp", []);
ssbApp.controller('ssbCtrl', function ($scope, $http) {
$scope.showHideTest = false;
});
Run Code Online (Sandbox Code Playgroud) 当用户点击谷歌地图上的某个点时,我会反向地理编码,如下所示:
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[1]) {
console.log(results[1]);
Run Code Online (Sandbox Code Playgroud)
在此阶段,结果[1]的控制台日志看起来像
Object {address_components: Array[4], formatted_address: "Rathmines, Co. Dublin, Ireland", geometry: Object, place_id: "ChIJN6MDC6kOZ0gRIhArCabX9o4", types: Array[2]}
Run Code Online (Sandbox Code Playgroud)
但是,当用户使用我弹出的地址点击infowindow时,我尝试将该对象传递给ng-click函数,如下所示:
ng-click='addressPicked("+results[1]+")'
Run Code Online (Sandbox Code Playgroud)
我收到了错误
Error: [$parse:syntax] Syntax Error: Token 'Object' is unexpected, expecting []]
at column 23 of the expression [addressPicked([object Object])] starting at [Object])].
Run Code Online (Sandbox Code Playgroud)
我在另一个应用程序中工作,但我只是通过一个lat&long字符串.
在尝试将对象作为参数传递之前,是否需要将对象转换为其他对象?
任何人在使用ng-click之前遇到此错误?
任何帮助将不胜感激,谢谢.
javascript google-maps geocoding angularjs angularjs-ng-click
另一个angularJS问题我有范围绑定点击,应该在第一次点击时添加一个值和第二次点击时的另一个值,但它只是保持返回并清空数组并再次填充第一个值为什么?:
scope.dbclickalert = function (scope, $watch) {
var getCheckInDate = this.cellData.date;
var formatcheckindate = new Date(getCheckInDate);
var checkingdates = [];
var curr_datecell = formatcheckindate.getDate();
var padded_day = (curr_datecell < 10) ? '0' + curr_datecell : curr_datecell;
var curr_monthcell = formatcheckindate.getMonth() + 1;
var padded_month = (curr_monthcell < 10) ? '0' + curr_monthcell : curr_monthcell;
var curr_yearcell = formatcheckindate.getFullYear();
var date_stringcell = + padded_month + "/" + padded_day + "/" + curr_yearcell;
var checkindatestrg = "checkindate";
console.log(checkingdates.length);
if (checkingdates.length < 2) …Run Code Online (Sandbox Code Playgroud) 我正在关注这篇文章:如何使用多个链接过滤AngularJS中的列表
现在我想在点击时显示由多个参数过滤的数据.
HTML
<div ng-app>
<span ng-click="myFilter = {type: 1}">Type 1</span> |
<span ng-click="myFilter = {type: 2}">Type 2</span> |
<span ng-click="myFilter = {type: 3}">Type 3</span> |
<!-- multiple filter - not working -->
<span ng-click="myFilter = [{type: 1}, {type:3}]">Types 1 & 3</span> |
<span ng-click="myFilter = null">No filter</span>
<ul ng-controller="Test">
<li ng-repeat="person in persons | filter:myFilter">{{person.name}}</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
JS
function Test($scope) {
$scope.persons = [{type: 1, name: 'Caio'}, {type:2, name: 'Ary'}, {type:1, name: 'Camila'}, , {type:3, name: 'Daniel'}];
} …Run Code Online (Sandbox Code Playgroud) 我将绑定参数传递给ng-click上的函数,如下所示:
<li ng-repeat="follwing in following.Users">
<a href="" ng-click="unfollow({{follwing.ID}})">Un-follow</a>
</li>
Run Code Online (Sandbox Code Playgroud)
这导致以下错误:
Error: [$parse:syntax] Syntax Error: Token 'follwing.ID' is unexpected, expecting [:] at column 12 of the expression [unfollow({{follwing.ID}})] starting at [follwing.ID}})].
http://errors.angularjs.org/1.2.10/$parse/syntax?p0=follwing.ID&p1=is%20unexpected%2C%20expecting%20%5B%3A%5D&p2=12&p3=unfollow(%7B%7Bfollwing.ID%7D%7D)&p4=follwing.ID%7D%7D)
at http://localhost/mb-www2015/js/angular.v1.2.10.js:78:12
at Parser.throwError (http://localhost/mb-www2015/js/angular.v1.2.10.js:9884:11)
at Parser.consume (http://localhost/mb-www2015/js/angular.v1.2.10.js:9921:12)
Run Code Online (Sandbox Code Playgroud)
当我添加单引号时{{follwing.ID}}:
<a href="" ng-click="unfollow('{{follwing.ID}}')">Un-follow</a>
Run Code Online (Sandbox Code Playgroud)
它按{{follwing.ID}}原样传递字符串,而不是其值.
还需要从unfollow更改文本和调用函数以遵循.如何从取消关注功能访问单击的锚标记?
谢谢你的帮助.
我想在项目列表的最后一项上添加点击事件.
<li ng-repeat="item in items" ng-click="myFunction" >{{item.name}}</li>
$scope.myFunction = function(){
// logic here...
}
Run Code Online (Sandbox Code Playgroud) angularjs ×10
javascript ×6
filter ×1
geocoding ×1
google-maps ×1
html ×1
ng-repeat ×1
operators ×1
splice ×1