AngularJS:指令没有启动或工作
AngularJS: Directive is not firing or working
新角度。所以不明白为什么下面的代码和指令不起作用。我在代码中出了问题。
页面中没有显示商品名称和价格。
几个问题
require:'ngModel'的含义是什么?
指令中的控制器是什么?
控制器选项何时启动?
当人们在指令中声明控制器选项时?
请详细分享知识?
Html代码:
<div ng-app="myApp">
<ul ng-controller="MyController">
<li my-directive price="item.price" ng-repeat="item in products">
{{item.name}} — {{item.price}}
</li>
</ul>
</div>
角度控制器:
var myApp = angular.module('myApp', []);
myApp.controller('MyController', function MyController($scope) {
$scope.products = [
{
'name' : 'Xbox',
'clearance' : true,
'price' : 30.99,
},
{
'name' : 'Xbox 360',
'clearance' : false,
'salesStatus' : 'old',
'price' : 99.99,
},
{
'name' : 'Xbox One',
'salesStatus' : 'new',
'price' : 50,
},
{
'name' : 'PS2',
'clearance' : true,
'price' : 79.99,
},
{
'name' : 'PS3',
'salesStatus' : 'old',
'price' : 99.99,
},
{
'name' : 'PS4',
'salesStatus' : 'new',
'price' : 20.99,
}
]
})
角度指令:
myApp.directive('myDirective', function(){
return {
scope: { price: '=' },
require: 'ngModel',
link : function(scope){
console.log(scope.price)
alert('scope price '+scope.price);
},
controller: function(scope, element, attrs, ngModel){
console.log(ngModel.price);
console.log(scope.price);
alert('ngModel price '+scope.price);
alert('scope price '+scope.price);
},
template: 'Name: {{item.name}} Address: {{item.price}}'
}
});
jsfiddle
require:'ngModel'的含义是什么?
当您需要another directive
的控制器时。在这里,您正在尝试调用ngModel
指令的控制器。
指令中的控制器是什么?控制器选项何时启动?什么时候人们在指令中声明控制器选项?
一个指令的Controller
是在自己的上下文中定义的,它可以注入到inter-directive communication
的其他指令中。
这里有一篇关于指令生命周期执行的详细帖子,可以帮助你做得更好。http://odetocode.com/blogs/scott/archive/2014/05/28/compile-pre-and-post-linking-in-angularjs.aspx
你的小提琴里有很多错误,你已经分享了,已经纠正了一些错误,打印了从view
传递到directive
的值来显示。https://jsfiddle.net/6am7xd0r/2/
相关文章:
- 重新启动jquery脚本后,角度停止工作
- 为什么不't在iOS上启动按钮下拉工作
- 重新启动游戏jQuery功能不工作
- 服务器重新启动后javascript不工作
- AngularJS:指令没有启动或工作
- 脚本工作,但永远不要启动它
- 启动日期选择器beforeShowDay只有在第一次点击后才能工作
- blur() 无法以编程方式工作 |但在从用户启动时会这样做
- 我的代码在没有警报()的情况下无法正常工作;启动函数
- ionios应用程序卡在启动屏幕上-在模拟器中工作,而不是在iphone5s设备上
- 功能重新启动工作不正确
- AngularJS UI启动日期选择器工作不正常
- 定时器功能在多次停止和启动时无法正常工作
- 启动和停止计时器不工作
- 我如何使计时器启动&只有当onFocus为true时才能工作
- 触摸启动事件未在原来隐藏的按钮上工作
- JQuery背景颜色更改(.animate或.css)在重新启动后只能工作一次
- Alfresco:更改“启动工作流”;按钮的文本
- 错误通过JavaScript启动工作流
- Alfresco社区版-5.1.x如何使用Webscript(Java或JavaScript)启动工作流