无法使用$ postLink向元素添加属性-AngularJS

时间:2019-01-08 16:35:45

标签: angularjs angularjs-components

我有一个简单的组件,我想做的就是动态地向输入元素添加属性var webHostBuilder = WebHost.CreateDefaultBuilder() .UseStartup<Startup>() .ConfigureServices(services => { services.AddSingleton<IStartupConfigureServicesFilter>(new MockInjectorFilter(s => { s.AddScoped(p => topicWriterFake); }); }); ,但是为什么它不起作用。为什么?有什么办法可以做我想要的吗?

public class MockInjectorFilter : IStartupConfigureServicesFilter
{
    private readonly Action<IServiceCollection> _configureServices;

    public ConfigureTestServicesStartupConfigureServicesFilter(Action<IServiceCollection> configureServices)
        => _configureServices = configureServices ?? throw new ArgumentNullException(nameof(configureServices));

    public Action<IServiceCollection> ConfigureServices(Action<IServiceCollection> next)
        => serviceCollection =>
        {
            next(serviceCollection);
            _configureServices(serviceCollection);
        };
}

1 个答案:

答案 0 :(得分:0)

这是解决方案:

app.component('myComponent', {
    templateUrl: 'tmpl.html',
    bindings: {
        str: '@'
    },
    controller: function ($element) {
        var ctrl = this;
        ctrl.$postLink = function () {
            $element.find('input').attr('multiple', 'multiple');
        }
    }
}

诀窍在于链接内容。问题中的代码不起作用,因为input钩子触发时$postLink在DOM中尚不存在。因此,我们需要将$element服务注入控制器并使用它来操作DOM。