Angular 1.0.1 vs 1.3.0 for directives

夙愿已清 提交于 2019-12-10 11:58:35

问题


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 qImgdirective, 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!