从 ng-repeat oddity 内部的 ng-click 访问事件对象
Accessing event object from ng-click inside of ng-repeat oddity
http://jsfiddle.net/5mbdgau1/3/
在我的 fiddle 示例中,我访问了通过 ng-click
传入的事件对象。它工作正常,但在处理来自 "OtherService" 的承诺时访问它时它突然不存在了?这是怎么回事?
使用 jQuery 有效,所以我认为这与 Angular jQlite 有关。此外,它会在 ng-repeat
内部中断,否则它会起作用。我想知道为什么事件对象似乎消失了。谢谢。
var myApp = angular.module('myApp',[]);
myApp.controller('TestController', function(TestService) {
var vm = this;
vm.foo = TestService;
vm.items = [1, 2, 3, 4];
});
myApp.service('OtherService', function($http) {
return $http.get('https://httpbin.org/get').then(function() {});
});
myApp.service('TestService', function(OtherService) {
return function(evt) {
var myText = evt.currentTarget.textContent;
evt.currentTarget.textContent = "Bar";
OtherService.then(function() {
console.log(evt);
evt.currentTarget.textContent = myText + myText;
});
}
});
html:
<div ng-controller="TestController as vm">
<div ng-repeat="item in vm.items">
<div ng-click="vm.foo(item, $event)">{{item}}</div>
</div>
</div>
您以错误的顺序传递了来自 ng-click
的参数。从
更改 ng-click
<div ng-click="vm.foo(item, $event)">{{item}}</div>
到
<div ng-click="vm.foo($event, item)">{{item}}</div>
您的 TestService
函数首先需要一个事件对象。
// Code goes here
var myApp = angular.module('myApp',[]);
myApp.controller('TestController', function(TestService) {
var vm = this;
vm.foo = TestService;
vm.items = [1, 2, 3, 4];
});
myApp.service('OtherService', function($http) {
return $http.get('https://httpbin.org/get').then(function() {});
});
myApp.service('TestService', function(OtherService) {
return function(item, evt) {
console.log(evt, item)
var myText = evt.currentTarget.textContent;
evt.currentTarget.textContent = "Bar";
OtherService.then(function() {
console.log(evt);
evt.currentTarget.textContent = myText + myText;
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="TestController as vm">
<div ng-repeat="item in vm.items">
<div ng-click="vm.foo(item, $event)">{{item}}</div>
</div>
</div>
http://jsfiddle.net/5mbdgau1/3/
在我的 fiddle 示例中,我访问了通过 ng-click
传入的事件对象。它工作正常,但在处理来自 "OtherService" 的承诺时访问它时它突然不存在了?这是怎么回事?
使用 jQuery 有效,所以我认为这与 Angular jQlite 有关。此外,它会在 ng-repeat
内部中断,否则它会起作用。我想知道为什么事件对象似乎消失了。谢谢。
var myApp = angular.module('myApp',[]);
myApp.controller('TestController', function(TestService) {
var vm = this;
vm.foo = TestService;
vm.items = [1, 2, 3, 4];
});
myApp.service('OtherService', function($http) {
return $http.get('https://httpbin.org/get').then(function() {});
});
myApp.service('TestService', function(OtherService) {
return function(evt) {
var myText = evt.currentTarget.textContent;
evt.currentTarget.textContent = "Bar";
OtherService.then(function() {
console.log(evt);
evt.currentTarget.textContent = myText + myText;
});
}
});
html:
<div ng-controller="TestController as vm">
<div ng-repeat="item in vm.items">
<div ng-click="vm.foo(item, $event)">{{item}}</div>
</div>
</div>
您以错误的顺序传递了来自 ng-click
的参数。从
ng-click
<div ng-click="vm.foo(item, $event)">{{item}}</div>
到
<div ng-click="vm.foo($event, item)">{{item}}</div>
您的 TestService
函数首先需要一个事件对象。
// Code goes here
var myApp = angular.module('myApp',[]);
myApp.controller('TestController', function(TestService) {
var vm = this;
vm.foo = TestService;
vm.items = [1, 2, 3, 4];
});
myApp.service('OtherService', function($http) {
return $http.get('https://httpbin.org/get').then(function() {});
});
myApp.service('TestService', function(OtherService) {
return function(item, evt) {
console.log(evt, item)
var myText = evt.currentTarget.textContent;
evt.currentTarget.textContent = "Bar";
OtherService.then(function() {
console.log(evt);
evt.currentTarget.textContent = myText + myText;
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="TestController as vm">
<div ng-repeat="item in vm.items">
<div ng-click="vm.foo(item, $event)">{{item}}</div>
</div>
</div>