[英]AngularJS: Directive is not firing or working
new in angular. 新的角度。 so just do not understand why the below code and directive is not working.
所以只是不明白为什么下面的代码和指令不起作用。 where i made the problem in code.
我在代码中出现问题的地方。
no item name and price is showing in page. 页面上没有显示商品名称和价格。
what is the meaning of require: 'ngModel', ? require:'ngModel',是什么意思?
what is controller in directive ? 指令中的控制器是什么?
when controller option fire ? 控制器选件何时触发?
when people declare controller option in directive ? 当人们在指令中声明控制器选项时?
share the knowledge please in details ? 请详细分享知识吗?
Html code: 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>
Angular Controller: 角度控制器:
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,
}
]
})
Angular Directive: 角度指令:
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}}'
}
});
what is the meaning of require: 'ngModel', ?
require:'ngModel',是什么意思?
When you want to require controller of another directive
. 当您需要
another directive
控制器时。 Here you are trying to call ngModel
directive's controller. 在这里,您尝试调用
ngModel
指令的控制器。
what is controller in directive ?
指令中的控制器是什么? when controller option fire ?
控制器选件何时触发? when people declare controller option in directive ?
当人们在指令中声明控制器选项时?
Controller
for a directive is defined in one's context, it can be injected in other directive for a inter-directive communication
. 指令的
Controller
是在一个人的上下文中定义的,可以将其注入其他指令中以inter-directive communication
。
Here is a detailed post on the life-cycle execution of a directive, which can help your better. 这是有关指令生命周期执行的详细信息,可以帮助您更好。 http://odetocode.com/blogs/scott/archive/2014/05/28/compile-pre-and-post-linking-in-angularjs.aspx
http://odetocode.com/blogs/scott/archive/2014/05/28/compile-pre-and-post-linking-in-angularjs.aspx
There is lots of mistakes in your fiddle you have shared, have corrected some to print the values passed from view
to directive
to display. 您分享的小提琴中有很多错误,更正了一些错误,以打印从
view
传递到directive
以显示的值。 https://jsfiddle.net/6am7xd0r/2/ https://jsfiddle.net/6am7xd0r/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.