myApp
.directive('timeInput', function(){
return {
restrict : 'A',
scope : {
ngModel : '='
},
link: function (scope) {
if (scope.ngModel) scope.ngModel = new Date(scope.ngModel);
}
}
});
scope里面的这个ngModel不是指的template中元素里面的ng-model吗?比如下面这个例子,scope: {name: '@myName',age: '=',changeAge: '&changeMyAge'}里面的三个name,age,changeAge都是中template模板里才引用的,上面的空模板是咋个意思?
我的名字是:
我的年龄是:
angular.module("MyApp", [])
.controller("MyController", function ($scope) {
$scope.name = "dreamapple";
$scope.age = 20;
$scope.changeAge = function(){
$scope.age = 0;
}
})
.directive("myDirective", function () {
var obj = {
restrict: "AE",
scope: {
name: '@myName',
age: '=',
changeAge: '&changeMyAge'
},
replace: true,
template: "" +
"
下面部分是我们创建的指令生成的
" +
"我的名字是:
" +
"我的年龄是:
" +
"在这里修改名字:
" +
"" +
" "
}
return obj;
});
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
在编写指令的时候,不一定都要指定模板的,比如像ngShow这样的内置指令,有时只是需要做一些简单的交互操作并不会涉及到模板
angular 的指令和组件分的不是很清楚, 这方面vue 分的比较清楚, 组件可以说是定义有另外功能的html标签.你说的不要模板的应该是指令, 那些要模板的 我更倾向用认为他是组件