AngularJS1.2 Directive双向绑定属性未反映到View
AngularJS1.2 Directive two Way Binding Attribute is not reflected to View
我不能在视图中使用scope变量中明显可用的scope属性。
请参阅plnkr:http://plnkr.co/edit/aoRU6YDywA0Q25gKpQGz?p=preview
script.js
// Code goes here
angular.module('myapp', [
'myapp.directive',
'myapp.controller'
]);
angular.module('myapp.controller', []).controller('mylist', function($scope) {
'use strict';
$scope.mylist = [{
name: "peter",
likes: 10
}, {
name: "bob",
likes: 2
}];
$scope.testme = 'fdsds';
});
angular.module('myapp.directive', []).directive('pmTable', function factory() {
var directiveDefinitionObject = {
scope: {
data: '=myvar'
},
controller: function($scope, $element, $attrs, $transclude) {
// console.log($scope);
},
compile: function compile(tElement, tAttrs, transclude) {
// Note: The template instance and the link instance may be different objects if the template has been cloned.
// For this reason it is not safe to do anything other than DOM transformations that apply to all cloned DOM nodes within the compile function.
// Specifically, DOM listener registration should be done in a linking function rather than in a compile function.
console.log('inside compile');
return {
pre: function preLink(scope, iElement, iAttrs, controller) {
console.log('preLink');
console.log(scope);
console.log(scope.data); // available here
scope.testme = 'testhere';
// scope.$apply(); // using that one doesn change anything
},
post: function postLink(scope, iElement, iAttrs, controller) {
console.log('postLink');
console.log(scope);
console.log(scope.data); // available here
console.log(scope.testme); // available from the parent scope
// scope.$apply(); // using that one doesn change anything
}
};
}
};
return directiveDefinitionObject;
});
index.html
<!DOCTYPE html>
<html data-ng-app="myapp">
<head>
<script data-require="angular.js@1.2.3" data-semver="1.2.3" src="http://code.angularjs.org/1.2.3/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div ng-controller="mylist">
{{mylist|json}}
{{ testme }}
<h1>Hello Plunker!</h1>
<div pm-table myvar="mylist">
From parent Scope: {{ testme }} <br />
{{myvar|json}}<br />
{{data|json}}<br />
<!-- this following repeat is not working -->
<tr data-ng-repeat="item in data">
<td>Name: {{ item.name }}</td>
</tr>
</div>
</div>
</body>
</html>
pm-table
是一个属性。指令的作用域仅限于此属性(或元素),因为您决定使用独立的作用域。CCD_ 2输出来自指令的作用域的值CCD_。
From parent Scope: {{ testme }} <br />
将打印控制器范围指定的fdsds
。该指令在此不适用。当然,ng-repeat
也是如此。data
在指令的范围内,此处不适用。
最简单的解决方案是使用scope: true
继承作用域。其他任何事情都需要额外的工作。
相关文章:
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 全局变量和全局对象的属性之间有什么区别吗
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 序列化数据属性中对象的最可靠方法
- 分析高度属性时出现意外值{{specs.height}}.index.html
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- AngularJS-使用'true'属性
- Ember.js/HHandlebars:使用{{view}}助手渲染的视图不绑定属性
- 如何在javascript中添加一个具有checkbox属性的变量,该变量将填充view.php中的一个表
- AngularJS1.2 Directive双向绑定属性未反映到View
- 设置Backbone.View的动态css样式属性
- 绑定observable的子属性时,未获取View中的所有值
- Parse.View.extend - "无法读取属性'extend'undefined&qu
- View确定模型的哪个属性被更改了
- 在Ember中,不能在View's ObjectController上设置attributeBindings属性
- Razor View Page html按钮onclick属性将无法识别嵌套代码块中的变量
- 在Backbone.View中获取模板的计算CSS属性
- 主干console.log属性,并将它们传递给View