我发现在使用元素src标记加载视频时处理错误与使用<video>元素加载视频时处理错误之间存在一些差异<source>.
例如,如果我尝试使用元素的src标记加载未找到的视频流(HTTP 404)video,则会触发事件并且元素存储错误数据:
HTML
<video src="http://not.found.url"></video>
Run Code Online (Sandbox Code Playgroud)
JS
var video = document.querySelector('video');
video.addEventListener('error', function(evt) {
console.log(evt.target.error); // Object
});
video.load();
Run Code Online (Sandbox Code Playgroud)
的video元件存储MediaError在对象error:
error: {
code: 4,
message: 'MEDIA_ELEMENT_ERROR: Format error'
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用source元素加载相同的视频流时:
HTML
<video>
<source src="http://not.found.url">
</video>
Run Code Online (Sandbox Code Playgroud)
JS
var video = document.querySelector('video');
var source = document.querySelector('source');
video.addEventListener('error', function(evt) {
// This event is not triggered
console.log(evt.target.error); // null
});
source.addEventListener('error', function(evt) {
console.log(evt.target.error); // …Run Code Online (Sandbox Code Playgroud) 我是AngularJS的新用户,当我必须从ng-repeat中的指令访问控制器中的变量时,我会陷入困境.我认为它必须与范围有关.
这是我的小提琴:http://jsfiddle.net/Zzb58/1/
'MyCtrl'范围有两个属性:"items",一个数组和"thing",只是一个字符串.
function MyCtrl($scope) {
$scope.items = [{id: 'item1'}, {id: 'item2'}];
$scope.thing = "thing";
}
Run Code Online (Sandbox Code Playgroud)
所以,如果我创建一个指令并且我希望它能够读取数组的内容,那么这个指令非常有效:
<div my-directive ng-repeat="item in items" item='item'></div>
Run Code Online (Sandbox Code Playgroud)
app.directive('myDirective', function () {
return {
restrict: 'AE',
scope: {
item: '=item',
},
template: '<div>{{ item.id }}</div>'
}
});
Run Code Online (Sandbox Code Playgroud)
HTML已正确刷新.
但是如果我尝试从指令中访问"thing"变量,它总是被读作"undefined".
app.directive('myDirective', function () {
return {
restrict: 'AE',
scope: {
item: '=item',
thing: '='
},
template: '<div>{{ item.id }} - Here is not the {{thing}}</div>'
}
});
Run Code Online (Sandbox Code Playgroud)
我认为问题必须与ng-repeat中创建的范围子项相关,或者变量未正确绑定.
但是,如果我在模板中使用$ parent.thing,则读取变量并对其进行正确计算,但我不知道该$ parent是否为ng-repeat范围或"MyCtrl"范围.
1)我做错了什么?
2)为什么需要将ng-repeat中读取的"item"元素作为HTML元素的属性?我首先认为"items"在父作用域中,所以当创建指令的隔离范围时,我只需要执行类似"items:'='"的操作.