问题
I build a simple directive with isolated scope.But it working fine with Version 1.0.1 but not in 1.3.0.What is the problem suppose to be ?
Angular 1.0.1 Version example : http://jsfiddle.net/k2rnavrg/
Angular 1.3.0 Version example :http://jsfiddle.net/k2rnavrg/2/
var myModule = angular.module('myModule', [])
.directive('qImg', function () {
return {
restrict: 'E',
scope: {
m: '=mF'
}
};
})
.controller('MyCtrl', ['$scope', function ($scope) {
$scope.foo = 'Hello Samitha!';
}]);
回答1:
You must not confuse directives with elements.
<q-img m-f="foo">
This is a q-img
element that also has a qImg
directive attached to it. The isolate scope created by this directive is only accessible to that directive, not to others, like the ngModel
directive.
<div ng-controller="MyCtrl">{{foo}}
<q-img m-f="foo">
<input type='text' ng-model="m">
</q-img>
ng-Controller
creates a scope that is used by all directives seen here, except for the qImg
directive, that uses an isolate scope.
If you want the ngModel
directive to access the isolate scope then you would need to put the input
into a template, not directly into the DOM:
var myModule = angular.module('myModule', [])
.directive('qImg', function () {
return {
restrict: 'E',
template: '<input type="text" ng-model="m">',
scope: {
m: '=mF'
}
};
})
<div ng-controller="MyCtrl">{{foo}}
<q-img m-f="foo"></q-img>
</div>
It seems that the behavior has changed with 1.2.
来源:https://stackoverflow.com/questions/25813595/angular-1-0-1-vs-1-3-0-for-directives