4

我正在尝试重现AngularUI的模态视图和动态分配控制器的角度材料行为的对话框视图,并能够将值作为参数发送

我试图模仿 AngularUI 代码但没有成功:

var elem =  document.getElementById('placeholder');
elem.setAttribute('ng-controller', 'ctrl');

var injector = angular.element(elem).injector();
var compile = injector.get('$compile');
var rootScope = injector.get('$rootScope');
var controller = injector.get('$controller');
var ctrlInstance = controller('ctrl', {$scope:rootScope, value:'hello'}); 
var result = compile(elem)(rootScope);

它适用于动态分配控制器而不传递值。我究竟做错了什么?

http://codepen.io/anon/pen/oXKvyb

4

1 回答 1

1

不要在元素上设置控制器。: http ://codepen.io/anon/pen/MwNgZx?editors=101

更新方法:

function setCtrl() {
    var elem =  document.getElementById('placeholder');
    //elem.setAttribute('ng-controller', 'ctrl');
    var eSpan = elem.children[0];
    var eSelect = elem.children[1];
    eSpan.setAttribute('ng-bind', 'name');

    eSelect.setAttribute('ng-options', 'o as o for o in rows');
    eSelect.setAttribute('ng-model', 'selectedValue');

    var injector = angular.element(elem).injector();
    var compile = injector.get('$compile');
    var rootScope = injector.get('$rootScope');
    var controller = injector.get('$controller');
  var result = compile(elem)(rootScope);  
  var ctrlInstance = controller('ctrl', {$scope:rootScope, value:'hello'});

}

此外,当您选择时,您正在做 children[1].children[0],这是不正确的。

我猜如果你ng-controller在元素上设置,angular 也会尝试实例化它,但不知道从哪里获取value参数。

编辑

我让这个例子做你想做的事情..我认为:)

于 2015-08-23T04:28:56.630 回答