0
我编写了一个角度js的自定义指令,该指令在plnkr, 中可用,当指令被加载时它已经调用MainCtrl上的changed()函数,我需要停止改变()函数,直到数据加载到指令中,并且在那之后,它应该开始观察指令内的输入变化,并且BTW变化的()函数应该在MainCtrl中,因为它在example中,所以在那里在将数据加载到指令之前停止执行ng-change的方法? 这里是代码:如何在自定义指令中加载数据后执行ng-change
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = "someName";
$scope.statusText = "unChanged";
$scope.changed = function(){
$scope.statusText = "changed";
};
});
app.directive("myDirective", function() {
return {
restrict: 'EA',
scope: {
ngModel: "=",
ngChange: "="
},
templateUrl: "template.html",
link: function ($scope, elem, attrs) {
}
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="[email protected]" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<my-directive ng-model="name" ng-change="changed()"></my-directive>
<p>{{statusText}}</p>
<script type="text/ng-template" id="template.html">
<input type="text" ng-model="ngModel" ng-change="ngChange" >
</script>
</body>
</html>
感谢Pengyy它工作正常。 – Aaron