How to specify model to a ngInclude directive in AngularJS?

前端 未结 6 1711
北海茫月
北海茫月 2020-11-30 02:48

I would like to use the same HTML template in 3 places, just each time with a different model. I know I can access the variables from the template, but there names will be d

6条回答
  •  北海茫月
    2020-11-30 03:30

    directive('newScope', function () { return { scope: true, priority: 450, compile: function () { return { pre: function (scope, element, attrs) { scope.$eval(attrs.newScope); } }; } }; });

    This is a directive that combines new-scope from John Culviner's answer with code from Angular's ng-init.

    For completeness, this is the Angular 1.2 26 ng-init source, you can see the only change in the new-scope directive is the addition of scope: true

    {
      priority: 450,
      compile: function() {
        return {
          pre: function(scope, element, attrs) {
            scope.$eval(attrs.ngInit);
          }
        };
      }
    }
    

提交回复
热议问题